无法验证,因为函数中返回的内容

时间:2014-09-17 19:40:57

标签: javascript jquery html

所以我试图用jQuery验证输入,这里看起来像什么

$(document).ready(function(){
    var name = $("#name1").val();
    var url = $("#url").val();
    var success = $("#success").val();
    var fail = $("#failure").val();
    var email = $("#email").val();

    $("#name1").on('keyup', function(){
        var name = $("#name1").val();
        console.log(name);
    });
    $("#url").on('keyup', function(){
        var url = $("#url").val();
        console.log(url);
    });

    $("#success").on('keyup', function(){
        var success = $("#success").val();
        console.log(success);   
    })
    $("#failure").on('keyup', function(){
        var fail = $("#failure").val();
        console.log(fail);
            if(success == fail){
        console.log(true);
    }
    })
    $("#email").on('keyup', function(){
        var email = $("#email").val();
        console.log(email);
    })

});

现在说我想检查名称和电子邮件是否相同,我怎么样?每当我执行所有操作时,它会在页面加载时得到处理。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

听起来你想检查它们是否与用户类型相同。你可以这样做:

    $(document).ready(function(){
    var name = $("#name1").val();
    var url = $("#url").val();
    var success = $("#success").val();
    var fail = $("#failure").val();
    var email = $("#email").val();

    $("#name1").on('keyup', function(){
        var name = $("#name1").val();
        console.log(name);
        checkEmailAndName();
    });
    $("#url").on('keyup', function(){
        var url = $("#url").val();
        console.log(url);
    });

    $("#success").on('keyup', function(){
        var success = $("#success").val();
        console.log(success);   
    })
    $("#failure").on('keyup', function(){
        var fail = $("#failure").val();
        console.log(fail);
            if(success == fail){
        console.log(true);
    }
    })
    $("#email").on('keyup', function(){
        var email = $("#email").val();
        console.log(email);
        checkEmailAndName();
    })

});

function checkEmailAndName()
{
    if($("#email").val() == $("#name1").val())
    {
        //Handle if they are the same
    }
    else
    {
        //Handle if they are different
    }
}