onclick 验证表单,如果有效只提交表单
Onclick validate form, if valid only submit form
我有一个 html 表单,我首先希望使用 jQuery 验证库 jquery.validate.min.js 和 如果表单有效 进行验证,请提交表格到一个位置。
我尝试了以下操作:
<html>
<head>
<link rel="stylesheet" href="../../common/view/css/bootstrap.min.css" type="text/css">
<script src="../../common/view/js/jquery.js"></script>
<script src="../../common/view/js/bootstrap.min.js"></script>
<script src="../../common/view/js/jquery.validate.min.js"></script>
</head>
<body>
<form method="post" action="stock-c.php" id="issueform" name="issueform">
<input type="text" name="pid"/>
<input type="button" name="button1" id="button1"/>
<script type="text/javascript" src="js/issueValidation.js"></script>
<!--Modal-->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!--info to be displayed-->
<input type="submit" value="submit"/> <!--Explain1-->
</div>
</div>
</from>
</body>
</html>
issueValidation.js
$( document ).ready( function () {
$validator= $( "#issueform" ).validate( {
rules: {
pid: "required",
},
messages: {
pid: "Please enter a value",
},
errorElement: "em",
errorPlacement: function ( error, element ) {
// Add the `help-block` class to the error element
error.addClass( "help-block" );
if ( element.prop( "type" ) === "checkbox" ) {
error.insertAfter( element.parent( "label" ) );
} else {
error.insertAfter( element );
}
},
highlight: function ( element, errorClass, validClass ) {
$( element ).parents( ".col-sm-5" ).addClass( "has-error" ).removeClass( "has-success" );
},
unhighlight: function (element, errorClass, validClass) {
$( element ).parents( ".col-sm-5" ).addClass( "has-success" ).removeClass( "has-error" );
}
});
$('#button1').on('click', function() {
$("#issueform").valid(); //everything works upto here
if($validator.noOfInvalids()===0){ //this code doesn't work
$('#myModal').modal('toggle');
}
});
});
Explain1: 我将提交按钮放在表单标签内的模态 class 中,以便在单击时提交表单数据。
理想情况下,我想在验证表单之前停止提交。如果表单有效,我必须能够在模式中查看我的表单数据并单击其上的提交按钮,从而从提交开始。
我尝试使用 jQuery 验证插件中的 submitHandler(仅当表单中没有无效项时才执行)来初始化模式,但为此我需要更改 input[type="button"] 到 input[type="submit"] 因为处理程序只在提交按钮上工作。然后我在同一个表单中得到两个提交按钮,但表单没有正确提交。
请帮我解决这个问题。回顾一下,我需要验证输入到表单中的数据并以模式请求用户确认。如果用户以模式确认,则表单中的数据将被提交。如果我的方法是实现这个结果的不必要的方法,只要符合上述要求,也欢迎提出替代建议。谢谢。
Ideally I want to halt submission until form is validated.
这就是 jQuery 验证插件已经在做的事情。
您的代码...
$('#button1').on('click', function() {
$("#issueform").valid(); //everything works upto here
if($validator.noOfInvalids()===0){ //this code doesn't work
$('#myModal').modal('toggle');
}
});
As per docs、.valid()
也是 returns 布尔值,因此上面代码的点击处理程序部分可以大大简化...
$('#button1').on('click', function() {
if ($("#issueform").valid()) {
// do something here when the form is valid
$('#myModal').modal('toggle');
}
});
我知道你已经提到了这一点,但以下是为了未来读者的利益:
请注意,您的 click
处理程序是必需的,仅因为 input
是 type="button"
。
或者,当使用 type="submit"
时,不需要您的 click
处理程序,因为插件会自动捕获点击。此外,在这种情况下,当表单有效时,您将使用 the submitHandler
option 包含 运行 的任何代码。
我有一个 html 表单,我首先希望使用 jQuery 验证库 jquery.validate.min.js 和 如果表单有效 进行验证,请提交表格到一个位置。
我尝试了以下操作:
<html>
<head>
<link rel="stylesheet" href="../../common/view/css/bootstrap.min.css" type="text/css">
<script src="../../common/view/js/jquery.js"></script>
<script src="../../common/view/js/bootstrap.min.js"></script>
<script src="../../common/view/js/jquery.validate.min.js"></script>
</head>
<body>
<form method="post" action="stock-c.php" id="issueform" name="issueform">
<input type="text" name="pid"/>
<input type="button" name="button1" id="button1"/>
<script type="text/javascript" src="js/issueValidation.js"></script>
<!--Modal-->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!--info to be displayed-->
<input type="submit" value="submit"/> <!--Explain1-->
</div>
</div>
</from>
</body>
</html>
issueValidation.js
$( document ).ready( function () {
$validator= $( "#issueform" ).validate( {
rules: {
pid: "required",
},
messages: {
pid: "Please enter a value",
},
errorElement: "em",
errorPlacement: function ( error, element ) {
// Add the `help-block` class to the error element
error.addClass( "help-block" );
if ( element.prop( "type" ) === "checkbox" ) {
error.insertAfter( element.parent( "label" ) );
} else {
error.insertAfter( element );
}
},
highlight: function ( element, errorClass, validClass ) {
$( element ).parents( ".col-sm-5" ).addClass( "has-error" ).removeClass( "has-success" );
},
unhighlight: function (element, errorClass, validClass) {
$( element ).parents( ".col-sm-5" ).addClass( "has-success" ).removeClass( "has-error" );
}
});
$('#button1').on('click', function() {
$("#issueform").valid(); //everything works upto here
if($validator.noOfInvalids()===0){ //this code doesn't work
$('#myModal').modal('toggle');
}
});
});
Explain1: 我将提交按钮放在表单标签内的模态 class 中,以便在单击时提交表单数据。
理想情况下,我想在验证表单之前停止提交。如果表单有效,我必须能够在模式中查看我的表单数据并单击其上的提交按钮,从而从提交开始。
我尝试使用 jQuery 验证插件中的 submitHandler(仅当表单中没有无效项时才执行)来初始化模式,但为此我需要更改 input[type="button"] 到 input[type="submit"] 因为处理程序只在提交按钮上工作。然后我在同一个表单中得到两个提交按钮,但表单没有正确提交。
请帮我解决这个问题。回顾一下,我需要验证输入到表单中的数据并以模式请求用户确认。如果用户以模式确认,则表单中的数据将被提交。如果我的方法是实现这个结果的不必要的方法,只要符合上述要求,也欢迎提出替代建议。谢谢。
Ideally I want to halt submission until form is validated.
这就是 jQuery 验证插件已经在做的事情。
您的代码...
$('#button1').on('click', function() {
$("#issueform").valid(); //everything works upto here
if($validator.noOfInvalids()===0){ //this code doesn't work
$('#myModal').modal('toggle');
}
});
As per docs、.valid()
也是 returns 布尔值,因此上面代码的点击处理程序部分可以大大简化...
$('#button1').on('click', function() {
if ($("#issueform").valid()) {
// do something here when the form is valid
$('#myModal').modal('toggle');
}
});
我知道你已经提到了这一点,但以下是为了未来读者的利益:
请注意,您的 click
处理程序是必需的,仅因为 input
是 type="button"
。
或者,当使用 type="submit"
时,不需要您的 click
处理程序,因为插件会自动捕获点击。此外,在这种情况下,当表单有效时,您将使用 the submitHandler
option 包含 运行 的任何代码。