【问题标题】:Disable submit button if checkbox is not checked in jquery如果未在 jquery 中选中复选框,则禁用提交按钮
【发布时间】:2012-04-13 01:09:14
【问题描述】:

我在这里查看并尝试了所有可用的答案,但它仍然不适合我。如果未选中复选框,我正在尝试禁用提交按钮。这是我的jQuery:

var userTerms = $('input#agree');
     if(userTerms.is(':checked')) {
        $("#aggtxt").addClass("err");
        $("#aggtxt").removeClass("error");
        $("#aggtxt").text(" Agreed");
        $("#bookit").attr('disabled', false);
        }else{
        $("#aggtxt").removeClass("err");
        $("#aggtxt").addClass("error");
        $("#aggtxt").text(" Please check terms and conditions box");
        $("#bookit").attr('disabled', true);
        }

         if ($('#bookit').is(':disabled') == true) { 
            $("#booktxt").removeClass("err");
            $("#booktxt").addClass("error");
            $("#booktxt").text(" Before you can submit this form please check the errors in form marked in red.");
            }else{
                $("#booktxt").addClass("err");
                $("#booktxt").removeClass("error");
                $("#booktxt").text(" Your information looks good, continue to booking...");
                }   
});

以及我的 HTML 表单的一部分:

<p><span id="aggtxt"></span><br /><input type="checkbox" name="agree" id="agree" />  I agree to <a href="http://travel.ian.com/index.jsp?pageName=userAgreement&locale=en_US&cid=379849" target="_blank">Terms and Conditions</a> and understand Cancellation Policy.<p>
<span id="booktxt"></span>
<input type="submit" name="bookit" value="" class="bookit" id="bookit" />

【问题讨论】:

    标签: jquery forms checkbox checked


    【解决方案1】:

    我对此的看法:

    http://jsfiddle.net/bluz/peans/

    希望这会有所帮助:) 帕兹。

    【讨论】:

    【解决方案2】:

    试试这个:

    $("#bookit").attr('disabled', 'disabled');
    

    我试过了:

    <input type="checkbox" name="agree" id="agree" onclick="$('#bookit').attr('disabled', !$(this).is(':checked'));" />
        I agree to <a href="http://travel.ian.com/index.jsp?pageName=userAgreement&locale=en_US&cid=379849" target="_blank">Terms and Conditions</a> and understand Cancellation Policy.<p>
    

    如果取消选中复选框,它会禁用按钮。

    【讨论】:

    • 新热点!! $el.prop('disabled', true)
    • 禁用的按钮代码工作正常,因为我也有它与表单的其他部分..只有当我检查复选框是否被选中时才会失败。
    【解决方案3】:

    试试这个:

    if($('#agree').val() == 'true')
    

    更多关于 jQuery 的信息documentation

    更正

    is(':checked') 方法仅在选中单选框/复选框时返回。

    【讨论】:

    • @liveandream 我现在更正了,你应该测试一下复选框是否被选中。
    猜你喜欢
    • 1970-01-01
    • 2020-03-20
    • 2013-08-12
    • 2012-04-18
    • 2015-09-22
    • 1970-01-01
    • 2021-11-22
    • 2019-05-23
    • 1970-01-01
    相关资源
    最近更新 更多