jQuery验证:阻止表单提交
问题描述:
通过使用jQuery插件Validation,我正在尝试验证表单.验证通过后,我希望JavaScript用表单中的值触发,然后停止.没有浏览到新网站/相同网站的实际表单提交.
By the use of the jQuery plugin Validation, I am trying to, well, validate my form. When the validation passes, I want a javascript to fire with values from the form and then stop. no actual form submission with browsing to a new/same site.
使用jQuery验证是否可能?
is this possible with jquery validation?
答
您可以尝试使用此方法(示例]) :
You may try this (Example):
$(function(){
$("#myform").validate();
$("#myform").on('submit', function(e) {
var isvalid = $("#myform").valid();
if (isvalid) {
e.preventDefault();
alert(getvalues("myform"));
}
});
});
function getvalues(f)
{
var form=$("#"+f);
var str='';
$("input:not('input:submit')", form).each(function(i){
str+='\n'+$(this).prop('name')+': '+$(this).val();
});
return str;
}