[英]prevent form submission when all input text fields are empty but allow submission when any of them in not empty with jquery
場景是我有一個帶有動態多個文本字段的表單。
<form id="add" action="" method="POST">
<input type="text" id="product-$id" name="quantity[]" class="quantity" />
</form>
根據條件,它會生成多個輸入字段。 看起來像這樣
<form id="add" action="" method="POST">
<input type="text" id="product-1" name="quantity[]" class="quantity" />
<input type="text" id="product-2" name="quantity[]" class="quantity" />
<input type="text" id="product-3" name="quantity[]" class="quantity" />
</form>
現在,如果所有文本字段均為空,我想阻止表單提交。 但是,如果其中任何一個有價值,我將允許提交表單。
我希望下面的代碼能給您一個思路,以進行下一步。
function SubmitForm(){
//get all the dynamic textbox in the form
var quantities =$('#add').find('.quantity');
var hasValue = false;
$.each(quantities, function(i, txtbox)
{
if ($.trim($(txtbox).val()) != '')
{
hasValue = true;
return false; //break
}
});
if (!hasValue)
{
return; //do not proceed further
}
//code here as atleast one textbox has a value
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.