【发布时间】:2016-11-23 06:38:49
【问题描述】:
我有一个 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');
}
});
});
说明1:我将提交按钮放在表单标签内的模态类中,以便在单击时提交表单数据。
理想情况下,我希望在表单得到验证之前停止提交。如果表单有效,我必须能够在模式中查看我的表单数据并单击其上的提交按钮,从而进行从提交。
我尝试在 jQuery 验证插件中使用 submitHandler(仅在表单中没有无效值时执行)来初始化模式,但为此我需要将 input[type="button"] 更改为 input[type ="submit"] 因为处理程序只对提交按钮起作用。然后我最终在同一个表单中有两个提交按钮,并且表单无法正确提交。
请帮我纠正这个问题。回顾一下,我需要验证输入到表单中的数据并以模式请求用户确认。如果用户以模态方式确认,则表单中的数据将被提交。如果我的方法是实现此结果的不必要方法,也欢迎其他建议,只要它符合上述要求。谢谢。
【问题讨论】:
-
document.forms['formID'].reportValidity()如果所有表单元素的有效性都为真,则返回真。 (对于非 jQuery 用户)
标签: jquery forms jquery-validate bootstrap-modal