[英]jquery validate multiple dynamically added fields
I am trying to validate multiple dynamically added fields on a form before submit using jquery but when one field is valid, the form is being submitted: http://jsfiddle.net/cvL0ymu7/ . 我正在尝试在使用jquery提交之前验证表单上的多个动态添加的字段,但是当一个字段有效时,正在提交表单: http : //jsfiddle.net/cvL0ymu7/ 。 How can I validate all fields before the submit.
如何在提交之前验证所有字段。
<html>
<body>
<form action="#" method="post">
<div id="fields"></div>
<input type="submit" value="Submit" />
</form>
<button id="test">Add field</button>
</body>
</html>
Le JavaScript JavaScript
$(function() {
$("#test").click(function(){
var unique_id = new Date().getTime();
$("#fields").append("<input class='myfield' type='text' name='myfield_" + unique_id + "'/><br />");
});
$( "form" ).submit(function( event ) {
if ( $( ".myfield" ).val() !== "" ) {
alert("form is valid");
//$( "span" ).text( "Validated..." ).show();
return;
}
//$( "span" ).text( "Not valid!" ).show().fadeOut( 1000 );
alert("form is invalid");
event.preventDefault();
});
});
Please check this fiddle ....this what you want to do... 请检查这个小提琴...这是您想做的...
enter code here
http://jsfiddle.net/cvL0ymu7/ enter code here
http://jsfiddle.net/cvL0ymu7/
Can you try MY CODE HERE 你能在这里尝试我的密码吗
$( "form" ).submit(function( event ) {
var vaild = true;
$('.myfield').each(function(){
if ($(this).val().trim() == '') {
vaild = false;
return;
};
});
if (valid) {
alert("form is valid");
} else {
alert("form is invalid");
event.preventDefault();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.