禁用/启用按钮 - jquery

时间:2014-08-03 03:40:58

标签: javascript jquery

我想禁用并启用提交输入类型,具体取决于用户使用jquery输入的完整性。我可以关闭它但不能打开它:(

$('#submit_btn').attr('disabled', 'disabled');

var nameFlag = false;;
var emailFlag = false;
var pwdFlag = false;

$('#f-name').focusout(function() {
    if($(this).val() == '') {
        nameFlag = false;
    } else if($(this).val().length < 4) {
        nameFlag = false;
    } else {
        nameFlag = true;
        //alert(nameFlag);
    }
});


$('#f-email').focusout(function() {
    if($(this).val() == '') {
        emailFlag = false;
    } else if($(this).val().length < 4) {
        emailFlag = false;
    } else {
        emailFlag = true;
        //alert(emailFlag);
    }
});


$('#f-pwd').focusout(function() {
    if($(this).val() == '') {
        pwdFlag = false;
    } else if($(this).val().length < 6) {
        pwdFlag = false;
    } else {
        pwdFlag = true;
        //alert(pwdFlag);
    }
});

//alert(nameFlag + " " + emailFlag + " " + pwdFlag);
if(nameFlag && emailFlag && pwdFlag) {
    $('#submit_btn').removeAttr('disabled');
} else {
    $('#submit_btn').attr('disabled', 'disabled');
}

所以我放了一些警告框,看到我的标志根据用户输入而改变,但它没有启用我的提交按钮

5 个答案:

答案 0 :(得分:2)

我也试过用jquery attr做同样的事情。它的工作正常。你在文件准备好了吗?

$(document).ready(function () {
    $('#submit_btn').attr('disabled', 'disabled');
    var nameFlag = false;
    var emailFlag = false;
    var pwdFlag = false;
    $("#fName").focusout(function () {
        if ($(this).val() == '') {
            DisabledButton();
        } else if ($(this).val().length < 4) {
            DisabledButton();
        } else {
            EnabledButton();
        }
    });


    $('#f-email').focusout(function () {
        if ($(this).val() == '') {
            DisabledButton()
        } else if ($(this).val().length < 4) {
            DisabledButton();
        } else {
            EnabledButton();
        }
    });

    function EnabledButton() {
        $('#submit_btn').removeAttr('disabled');
    }

    function DisabledButton() {
        $('#submit_btn').attr('disabled', 'disabled')
    }
});

请参阅小提琴链接http://jsfiddle.net/2jDKm/2/

答案 1 :(得分:1)

使用此:
http://jsfiddle.net/jq7Q7/1/
我将您的支票代码移动到一个新功能,并在每次聚焦控件时检查它:

    $('#submit_btn').attr('disabled', 'disabled');

    var nameFlag = false;;
    var emailFlag = false;
    var pwdFlag = false;

    $('#f-name').focusout(function() {
        if($(this).val() == '') {
            nameFlag = false;
        } else if($(this).val().length < 4) {
            nameFlag = false;
        } else {
            nameFlag = true;
            //alert(nameFlag);
        }
         checkform();
    });


    $('#f-email').focusout(function() {
        if($(this).val() == '') {
            emailFlag = false;
        } else if($(this).val().length < 4) {
            emailFlag = false;
        } else {
            emailFlag = true;
            //alert(emailFlag);
        }
         checkform();
    });


    $('#f-pwd').focusout(function() {
        if($(this).val() == '') {
            pwdFlag = false;
        } else if($(this).val().length < 6) {
            pwdFlag = false;
        } else {
            pwdFlag = true;
            //alert(pwdFlag);
        }
         checkform();
    });
    function checkform(){
        //alert(nameFlag + " " + emailFlag + " " + pwdFlag);
        if(nameFlag && emailFlag && pwdFlag) {
            $('#submit_btn').removeAttr('disabled');
        } else {
            $('#submit_btn').attr('disabled', 'disabled');
        }
    }

答案 2 :(得分:0)

你可以用prop()方法代替attr()

$('#submit_btn').prop('disabled', true);

$('#submit_btn').prop('disabled', false);

可以帮助您解决此问题

答案 3 :(得分:0)

这是我的HTML:

<form class="form_box prefix_clear" method="get" action="#login">
    <div class="f-field">
        <label for="f-name">Username:</label><br/>
        <input id="f-name" class="submit_text" type="text" name="username"/>
        <p class="f-error case1">Please enter your username</p>
        <p class="f-error case2">Your username has to be at least 4 characters</p>
    </div>
    <div class="f-field">
        <label for="f-email">Email:</label><br/>
        <input id="f-email" class="submit_text" type="text" name="email"/>
        <p class="f-error case1">Please enter your email</p>
        <p class="f-error case2">Your email is not a valid format</p>
    </div>
    <div  class="f-field">
        <label for="f-pwd">Password:</label><br/>
        <input id="f-pwd" class="submit_text" type="password" name="password"/>
        <p class="f-error case1">Please enter your password</p>
        <p class="f-error case2">Your password has to be at least 6 letters (A-Z, a-z, 0-9)</p>
    </div>
    <div class="prefix_clear">
        <input id="submit_btn" class="f-btn" type="submit" name="submit" value="Create Account"/>
    </div>
</form>

答案 4 :(得分:0)

你的变量有问题。试试这个代码它的工作正常。

$(document).ready(function () {
    $('#submit_btn').attr('disabled', 'disabled');

    var nameFlag = false;;
    var emailFlag = false;
    var pwdFlag = false;

    $('#f-name').focusout(function () {
        if ($(this).val() == '') {
            DisabledButton();
        } else if ($(this).val().length < 4) {
            DisabledButton();
        } else {
            EnabledButton();
            //alert(nameFlag);
        }
    });


    $('#f-email').focusout(function () {
        if ($(this).val() == '') {
            DisabledButton();
        } else if ($(this).val().length < 4) {
            DisabledButton();
        } else {
            EnabledButton();
            //alert(emailFlag);
        }
    });


    $('#f-pwd').focusout(function () {
        if ($(this).val() == '') {
            DisabledButton();
        } else if ($(this).val().length < 6) {
            DisabledButton();
        } else {
            EnabledButton();
        }
    });

    function EnabledButton() {
        $('#submit_btn').removeAttr('disabled');
    }

    function DisabledButton() {
        $('#submit_btn').attr('disabled', 'disabled')
    }
});

请参阅jsfiddle链接http://jsfiddle.net/2jDKm/4/