jQuery验证在调用时不起作用

jQuery validation not working when called

本文关键字:不起作用 调用 验证 jQuery      更新时间:2023-09-29

好的,所以我想使用jQuery验证插件来验证一个多部分的表单,一次验证一个页面。我确信我的大部分脚本都是正确的,但由于某些原因,当最初通过validateStep函数调用时,下面的脚本将不起作用。奇怪的是,一旦调用了validateStep函数,并且用户试图输入无效数据,验证就会工作。这几乎就像它只是在调用时打开验证,但一开始没有发现任何错误。有什么想法吗?为什么错误在被调用时不会咳嗽?

这是我的代码:

function validateStep(step){
    if(step == fieldsetCount) return;

$('#formElem').validate({
    rules: {
       Remail: {
        required: true,
        email: true
        }
    },
    messages: {
       Remail: {
        required: 'We need your email address to contact you',
        email: 'not right'
        }
    }
 }).element($('#formElem').children(':nth-child('+ parseInt(step) +')'));
});    

尝试:

var validator = $("#formElem").validate({
    rules: {
       Remail: {
        required: true,
        email: true
        }
    },
    messages: {
       Remail: {
        required: 'We need your email address to contact you',
        email: 'not right'
        }
    }
 });
if (validator.element("#formElem > :nth-child("+parseInt(step)+")")) {
    alert ("is valid");
}