JSFiddle - React, Tailwind, and code Playground
HTML
<input class="placeholder_required" value="hi_1" placeholder="hi_1">
<br>
<input class="placeholder_required" value="hi_2" placeholder="hi_2">
<br>
<input class="placeholder_required" value="hi_3" placeholder="hi_3">
<br>
<input id="add_input" class="" value="後來加的" placeholder="後來加的">
<br>
<input class="placeholder_optional" value="hi_4" placeholder="hi_4">
<br>
<textarea name="" id="" cols="30" rows="10" class="placeholder_optional" placeholder="text">text</textarea>
<button id="add">add</button>
<button id="submit">
submit
</button>
JavaScript
$(function() {
$("#add").click(function(){
$("#add_input").addClass('placeholder_required');
})
$("#submit").click(function() {
var chk = chk_palceholder();
//如果 chk 是true就往下送
alert(chk);
});
})
function chk_palceholder() {
//檢查必填
var chk = true;
$(".placeholder_required").each(function(k, v) {
if ($(this).val() == $(this).attr("placeholder")) {
chk = false;
}
})
//檢查非必填
$(".placeholder_optional").each(function(k, v) {
if ($(this).val() == $(this).attr("placeholder")) {
$(this).val('');
}
})
return chk;
}