我正在使用jquery验证器,输入字段保留为空白并将其值显示为空,但jquery valid()方法始终返回true。实际上,表单中有几个选项卡,下一个选项卡的下一个选项卡的每次单击都将显示,之前将隐藏。所以我们不能一次验证所有表单我的验证规则如下:
$('#wizard_example_1').validate({
rules: {
link_client_name: {
required: true
},
link_page_url: {
required: true,
url: true
},
link_title: {
required: true
},
link_message: {
required: true
},
link_text: {
required: true
},
link_url: {
required: true,
url: true
}
}
});
和jquery代码检查特定元素是否有效是
$('.next-btn').on('click', function (e) {
e.preventDefault();
count++;
if (obj['camp_type'] === "1") {
var valid = false;
$.each($('#link input'), function (i, v) {
var id=$(this).attr('name');
valid=$('input[name="' + $(this).attr('name') + '"]').valid();
});
if(valid) {
obj = {};
obj['camp_type'] = "1";
obj['client_name'] = $('#link_client_name').val();
obj['page_url'] = $('#link_page_url').val();
obj['title'] = $('#link_title').val();
obj['message'] = $('#link_message').val();
obj['action_text'] = $('#link_text').val();
obj['action_url'] = $('#link_action_url').val();
console.log(obj);
}else{
return false;
}
}
});
答案 0 :(得分:2)
尝试使用此方法实施。
var $ValidateForm = $('#wizard_example_1').validate({
rules: {
link_client_name: {
required: true
},
link_page_url: {
required: true,
url: true
},
link_title: {
required: true
},
link_message: {
required: true
},
link_text: {
required: true
},
link_url: {
required: true,
url: true
}
}
});
$('.next-btn').off();
$(document).on('click', '.next-btn', function () {
if ($ValidateForm.form()) {
// your logic
}
});