2013-09-25 127 views
0

我有以下HTML:jQuery:not(:checked)not working?

<form class="pricing-downloads-right pricing-last-form" action=""> 
    <div class="pricing-levels-top-2"> 
     <input class="pricing-single-level" type="radio" name="sex" value="male"><strong>Single Level Download $49.95:</strong> Choose a single level (1 to 7) and receive access to all lessons, PDFs and review audio for that level, plus 30 days of online access ($80 value). Email us your level choice after purchase.<br> 
     <input class="pricing-3-level-download" type="radio" name="sex" value="male"><strong>3 Level Download $99.95:</strong> Choose any 3 levels (1 to 7) and receive access to all lessons, PDFs and review audio for those levels, plus 60 days of online access ($190 value). Email us your level choices after purchase.<br> 
     <input class="pricing-all-access-package" type="radio" name="sex" value="male"><strong>All Access Package $199.9:</strong> Receive access to all lessons, PDFs and review audio for all 7 levels, plus 90 days of online access ($300 value).<br> 
    </div> 
    <div class="pricing-levels-2"> 
     <p>Which level would you like? (Click all that apply)</p> 
     <input type="checkbox" name="vehicle" value="Bike">Level 1<br> 
     <input type="checkbox" name="vehicle" value="Bike">Level 2<br> 
     <input type="checkbox" name="vehicle" value="Bike">Level 3<br> 
     <input type="checkbox" name="vehicle" value="Bike">Level 4<br> 
     <input type="checkbox" name="vehicle" value="Bike">Level 5<br> 
     <input type="checkbox" name="vehicle" value="Bike">Level 6<br> 
     <input type="checkbox" name="vehicle" value="Bike">Level 7<br> 
    </div> 
</form> 

和jQuery:

jQuery('.pricing-downloads-right input[type=radio]').on('change', function(){ 
    if(jQuery('.pricing-levels-top-2 input.pricing-single-level:checked').length > 0){     
      //since you don't want to target by name 
      jQuery('.pricing-levels-2').show(); 
    } 
}); 

jQuery('.pricing-downloads-right input[type=radio]').on('change', function(){ 
    if(jQuery('.pricing-levels-top-2 input.pricing-single-level:not(:checked)').lenght > 0){ 
      //since you don't want to target by name 
      jQuery('.pricing-levels-2').hide(); 
    } 
}); 

所以我想.pricing-levels-2input.pricing-single-level進行檢查,以顯示和隱藏,當它處於未選中狀態。

現在.pricing-levels-2顯示input.pricing-single-level被選中但未選中時不隱藏。

我做錯了嗎?

+1

你怎麼知道哪個值是從1級選擇第7級時複選框所有值都具有相同的價值,「自行車」 –

+0

更改複選框的值..提到的複選框只會產生「自行車」 –

回答

4

你拼錯length

if(jQuery('.pricing-levels-top-2 input.pricing-single-level:not(:checked)') 
    .lenght > 0){ 

應該

if(jQuery('.pricing-levels-top-2 input.pricing-single-level:not(:checked)') 
    .length > 0){ 
+1

哦,這是一個語法問題,而不是編程問題。 – alexchenco

4

嘗試

jQuery('.pricing-downloads-right input[type=radio]').on('change', function() { 
    jQuery('.pricing-levels-2').toggle(jQuery('.pricing-levels-top-2 input.pricing-single-level:checked').length > 0); 
}).change(); 

演示:Fiddle

一個可能更好的辦法

jQuery(function ($) { 
    var $pl2 = $('.pricing-levels-2'); 
    var $psl = $('.pricing-levels-top-2 input.pricing-single-level'); 

    $('.pricing-downloads-right input[type=radio]').on('change', function() { 
     $pl2.toggle($psl.is(':checked')); // $ps1[0].checked 
    }).change(); 

}) 

演示:Fiddle