2012-04-13 115 views
0

我在此处查看并尝试了每个可用的答案,但它仍然不适用于我。我试图禁用提交按钮,如果复选框未选中。这里是我的jQuery:如果未在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" /> 

回答

1

试试这个:

$("#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> 

和禁止如果取消选中该复选框,该按钮。

+0

前者也可以接受,不是问题。 – Ohgodwhy 2012-04-13 01:12:18

+0

新热点! '$ el.prop('disabled',true)' – elclanrs 2012-04-13 01:15:50

+0

nope :(没有工作 – liveandream 2012-04-13 01:18:15

0

试试这个:jQuery的documentation

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

更多信息。

纠错

is(':checked')方法,只有当被选中的无线/复选框返回。

+0

nope .. #bookit是提交按钮 – liveandream 2012-04-13 01:17:51

+1

@liveandream我现在纠正了它,你应该测试复选框是否被选中。 – SIFE 2012-04-13 01:20:54

7

我拿到这个:

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

希望这有助于:) 拉巴斯。

+1

你还没有使用“Var”在你的脚本中声明一个变量 这是正确的一个 http://jsfiddle.net/peans/39/ – 2013-04-28 17:24:56

+1

这适用于我的单选按钮!谢谢:) – Zl3n 2014-12-21 15:36:16

相关问题