2012-04-13 60 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

相關問題