The scenario is that I have a form with dynamic multiple text field.
<form id="add" action="" method="POST">
<input type="text" id="product-$id" name="quantity[]" class="quantity" />
</form>
depending on condition it generates multiple input fields. It looks like this
<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>
Now I want to prevent my form submission if all of the text fields are empty. But I will allow form submission if any of them have a value.
I hope the following code will give you an idea on how to proceed.
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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.