[英]jQuery form doesn't validate when button is outside of form tags
I can't seem to get my form to validate when the button is outside the <form></form>
tag. 当按钮位于
<form></form>
标记之外时,我似乎无法让我的表单进行验证。 Any ideas how I can achieve this? 有什么想法可以实现这一目标吗? I can't place it inside the form for technical reasons (it's inside a sticky navbar).
出于技术原因,我无法将其放置在表单中(它位于粘性导航栏中)。
My HTML markup: 我的HTML标记:
<form id="frm-shipping" class="frm-shipping" method="post">
<input type="text" name="contact_phone" class="has_validation"
placeholder="Contact phone" value="" data-validation="required"
data-validation-error-msg="this field is mandatory!" >
<input type="text" name="first_name" class="has_validation"
placeholder="First Name" value="" data-validation="required"
data-validation-error-msg="this field is mandatory!" >
</form>
<button class="btn" onclick="nextStep();">Next</button>
And the jQuery code: 和jQuery代码:
function nextStep()
{
$.validate({
form : '#frm-shipping',
borderColorOnError:"#FF0000",
onError : function() {
},
onSuccess : function() {
var params = $( "#frm-shipping").serialize();
// AJAX call here
}
});
}
When I click the button, nothing happens. 当我单击按钮时,什么也没有发生。 Simply nothing :( Any suggestions?
简直一无所有:(有什么建议吗?
You can assign the button to the form with form
attribute. 您可以将按钮分配给具有
form
属性的表单。 See W3Schools example . 请参阅W3Schools示例 。
<button class="btn" form="frm-shipping" onclick="nextStep();">Next</button>
function nextStep() { $.validate({ form : '#frm-shipping', borderColorOnError:"#FF0000", onError : function() { console.log('error') }, onSuccess : function() { console.log('te3st') var params = $( "#frm-shipping").serialize(); // AJAX call here } }); }
<script src="https://code.jquery.com/jquery-3.1.0.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/jquery-form-validator/2.3.26/jquery.form-validator.min.js"></script> <form id="frm-shipping" class="frm-shipping" method="post"> <input type="text" name="contact_phone" class="has_validation" placeholder="Contact phone" value="" data-validation="required" data-validation-error-msg="this field is mandatory!" > <input type="text" name="first_name" class="has_validation" placeholder="First Name" value="" data-validation="required" data-validation-error-msg="this field is mandatory!" > </form> <button class="btn" form='frm-shipping' onclick="nextStep();">Next</button>
Try this 尝试这个
$('.btn').click(function(){
Validate method
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.