0

我有一个表格,我正在使用 focusin 和 focusout 事件并验证 focusout。我一开始禁用了提交按钮,我想在验证后再次启用它。这是我的代码。

    $("document").ready(function() {
    $("#contact_submit").attr("disabled", "disabled");
    $("#first_name").focusin(function() {
        $("#Frist_name_comment").html('Please inter your first name.');
    }).focusout(function() {
        var first_name = $("#first_name").val();
        if(first_name == "") {
            $("#Frist_name_comment").html("First Name is required.");
        }else {
            $("#Frist_name_comment").html('OK');
        }
    });

    $("#last_name").focusin(function() {
        $("#Last_name_comment").html('Please inter your last name.');
    }).focusout(function() {
            var last_name = $("#last_name").val();
        if(last_name == "") {

            $("#Last_name_comment").html("Last Name is required.");
        }else {
            $("#Last_name_comment").html('OK');
        }
    });

    $("#email").focusin(function() {
        $("#Email_comment").html('Please inter a email address.');
    }).focusout(function() {
        var email = $("#email").val();
        var atpos = email.indexOf("@");
        var dotpos = email.lastIndexOf(".");
        if((email == "" || atpos < 1 || dotpos < atpos+2 || dotpos+2 > email.length)) {
            mess == "Not a vallied email.";
            $("#Email_comment").html("Not a valied email.");
        }else {
            $("#Email_comment").html('OK');
        }
    });

    // hear i want to enable submit button after all validation


});
4

2 回答 2

0
$("button").prop("disabled", true); <== for disabling

或者

$("button").prop("disabled", false); <== for enabling

disabled属性也可以是 BOOLEAN 来表示状态。jQuery 有.attr()

.prop()访问元素属性的函数。我用过.prop(),你也可以用

.attr().

于 2013-01-30T07:04:17.463 回答
0

你可以试试这个:

if($("#Last_name_comment).text()=='OK'|| 
   $('#Frist_name_comment).text()=='OK' || 
   $('#Last_name_comment').text()=='OK' || 
   $('#Email_comment").text()=='OK'){
  $("#contact_submit").removeAttr("disabled");
}

您必须检查OK字符串是否存在,然后删除 attrdisabled

于 2013-01-30T07:04:53.293 回答