2013-06-22 172 views
-1

我們如何選中或取消選中複選框上點擊所有單選按鈕。如果選中複選框,那麼所有單選按鈕也查了,反之亦然..它不能正常檢查單選按鈕上的複選框更改事件

<input type="checkbox" id="Check" />SelectAll<br /><input type="radio"/>First<br /> 


    <input type="radio"/>Second<br /> 
      <input type="radio"/>Third<br /> 
      <input type="radio"/>Fourth<br /> 
      <input type="radio"/>Fifth</div> 
      <script> 
       var IsCheck = false; 
       $(document).ready(function() { 
        $("#Check").change(function() { 
         if (IsCheck == false) { 
          $("input[type=radio]").attr("checked", true); 
          IsCheck == true 
         } 
         else { $("input[type=radio]").attr("checked", false); IsCheck == false } 
        }); 
       }); </script> 
+2

我不認爲廣播是正確的選擇HTTP:/ /en.wikipedia。org/wiki/Radio_button通常只選擇一個組中的一個選項 – HMR

+1

請不要對此使用單選按鈕 – Jonathan

+2

(重要)注意:'var IsCheck = false;'引入一個全局變量,[這是一件壞事]( http://stackoverflow.com/questions/10525582/why-are-global-variables-considered-bad-practice-javascript)。將你的JavaScript代碼封裝在[自我調用函數]中(http://stackoverflow.com/questions/15313163/javascript-self-invoking-function)。 –

回答

1

工作試試這個

$(document).ready(function() {      
    $("#Check").change(function() {       
     $("input[type=radio]").attr("checked", $("#Check").is(":checked"));        
    }); 
}); 

Demo

+0

謝謝你...它的工作... –

3

照顧你只是比較操作數,而不是在這個語句賦值給一個變量:

IsCheck == true 
     ^------ REMOVE ONE = so it's an assignment 

另外,不要使用.attr("checked", true);正確的形式是:

$("input[type=radio]").attr("checked", 'checked'); //checking for jQuery < 1.6 

並取消選中:

$("input[type=radio]").removeAttr("checked"); //unchecking for jQuery < 1.6 

如果您正在使用jQuery> 1.6,你可以使用.prop()使用布爾值的方法,與您嘗試使用它的方式類似:

$("input[type=radio]").prop("checked", true); //checking for jQuery >= 1.6 
$("input[type=radio]").prop("checked", false); //unchecking for jQuery >= 1.6 
1

對於你的問題,這可能是答案:

$("#Check").change(function() { 
    $("input:radio").prop("checked", this.checked); 
}); 

演示:http://jsfiddle.net/hungerpain/QSx29/

也就是說,單選按鈕都沒有這樣做的最佳方式。它的語義不是沒錯。您只能在一個組中選擇一個單選按鈕。嘗試使用複選框。試試你的標記改成這樣:

<input type="checkbox" id="Check" />SelectAll 
<br /> 
<input type="checkbox" />First 
<br /> 
<input type="checkbox" />Second 
<br /> 
<input type="checkbox" />Third 
<br /> 
<input type="checkbox" />Fourth 
<br /> 
<input type="checkbox" />Fifth</div> 

而更換JS代碼到這一點:

$("#Check").change(function() { 
    $("input:checkbox").prop("checked", this.checked); 
}); 

這裏有一個演示:http://jsfiddle.net/hungerpain/QSx29/1/

1

你只需要這個

$("#Check").change(function() { 
    $("input[type=radio]").prop("checked", this.checked); 
}); 

演示---->http://jsfiddle.net/kLnyD/

+0

-1使用單選按鈕作爲複選框 – Jonathan

+0

$(「input [type = radio]」)+1最有效的代碼和第一次使用道具 – HMR

+0

是的,我會撤銷-1爲最佳代碼... – Jonathan

2

jQuery的1.9或更高的使用

$('input[type=radio]').prop("checked", true) 

否則儘量

$('input[type=radio]').attr('checked', 'checked'); 
0

試試這個

http://jsfiddle.net/4u9sQ/

$("#Check").change(function() { 
         if ($(this).is(':checked')) { 
          $("input[type=radio]").prop("checked", true); 

         } 
         else { $("input[type=radio]").prop("checked", false); } 
        }); 
相關問題