2012-07-25 24 views
20

所有JQuery的無線電buttonset使用JQ-UI的buttonset功能取消選中一次

<script> 
    $(function() { 
     $("#radio").buttonset(); 
    }); 
    </script> 


    <div id="radio"> 
     <input type="radio" id="radio1" name="radio" /><label for="radio1">Choice 1</label> 
     <input type="radio" id="radio2" name="radio" checked="checked" /><label for="radio2">Choice 2</label> 
     <input type="radio" id="radio3" name="radio" /><label for="radio3">Choice 3</label> 
    </div> 

有什麼辦法來取消buttonset的所有單選按鈕一次?

+0

應該'$(「#radio」)'而不是'$(「radio」)'? – fcalderan 2012-07-25 09:43:16

+1

@Dev你曾經使用過jQ-UI嗎? – heron 2012-07-25 09:49:28

回答

31

可以取消他們他們用下面的(更新的jQuery UI 1.9:。

$('#radio input').removeAttr('checked'); 
// Refresh the jQuery UI buttonset.     
$("#radio").buttonset('refresh'); 
​ 

工作JSFiddle

+0

不錯。答案越來越短。 :) – albertjan 2012-07-25 09:49:15

+0

那個小提琴似乎沒有成功地將單選按鈕變成一個jQuery UI按鈕集,這就是爲什麼它似乎工作。 Frédéric說得不對,你得刷新一下。 – 2012-07-25 09:52:11

+0

這在jQuery 1.9中不起作用,不會影響jQuery UI按鈕集。下面的答案是更好的答案。 – CoryDorning 2013-03-07 15:40:54

15

可以匹配所有的單選按鈕,並使用prop()取消選中它們

但是,你也必須這樣做後刷新buttonset部件:

$("#radio").find("input:radio").prop("checked", false).end() 
      .buttonset("refresh"); 
+0

這很好! – karancan 2012-12-21 20:17:40

13

的jQuery 1.6版本之前

$(':radio').attr('checked', false); 

OR

$(':radio').removeAttr('checked'); 

jQuery的1.6+

$(':radio').prop('checked', false); 

OR

後0
1

偶然發現...使用jQuery 1.9.1使用buttonset類名留下所有按鈕最初未設置。還不確定是否有這種後果,但方便知道。

$("div.myclass").buttonset(); 

<div id="myDiv" class="myclass"> 
    <input type="radio" name="myname" id="id1" value="1"><label for="id1">Label1</label> 
    <input type="radio" name="myname" id="id2" value="2"><label for="id2">Label2</label> 
    <input type="radio" name="myname" id="id3" value="3"><label for="id3">Label2</label> 
</div>