2010-08-25 46 views
11

我可以預期讀取jQueryUI單選按鈕的值,但是,我無法以編程方式設置它。無論使用哪種方法更改單選按鈕的值,jQueryUI界面都不會更新其接口。jQueryUI Radio/Check按鈕不會以編程方式更改

<div id="radio"> 
    <input type="radio" name="radio" value="true" checked="checked" />Yes 
    <input type="radio" name="radio" value="false" />No 
</div> 

<script type="text/javascript"> 
    $('#radio').buttonset(); 

    // One of the many ways that won't work. 
    $('[name="radio"]:radio:checked').val("false"); 
</script> 
+0

你在哪裏使用jQuery UI? – 2010-08-25 20:07:36

回答

20

一旦你更新的價值,就像這樣:

$('[name="radio"][value="false"]').attr("checked", true); 

你需要告訴jQuery UI的更新,像這樣:

$('#radio').buttonset("refresh"); 

You can give it a try here

+0

當我回答時,問題的按鈕組部分不存在。 :/ – 2010-08-25 20:21:10

+0

我從來沒有想到這一點。謝謝。 – rebelliard 2010-08-25 20:30:19

+0

@Yanick - 這是......他從一開始就提到jQuery UI,這就是爲什麼我的評論甚至在你的回答詢問它之前發佈:) – 2010-08-25 20:32:29

4
$('[name="state"]:radio:checked').attr('checked', true); // checked 
$('[name="state"]:radio:checked').removeAttr('checked'); // unchecked 

** 注意 **

$(':radio').val(); // same as $(':radio').attr('value'); 

這樣:

$(':radio[checked]').val(); // -> the value of the first checked radio button found 
+0

我完全抱歉。考慮到常規的單選按鈕是非常簡單的,我只是覺得這樣的細節不會太多。 oO./ – rebelliard 2010-08-25 20:31:09

8

我通常做:

$('[name="radio"][value="false"]').attr("checked", true).trigger("change"); 

的buttonset(「刷新」)調用綁定到哪個當你編程改變值根本不會觸發更改事件 - 觸發它手動解決了用戶界面的問題,你不必擔心在buttonset是

1

我通常做:

$('[name="radio"][value="false"]').prop("checked", true).trigger("change"); 

請使用「託」,而不是「ATTR」改變「檢查」狀態。

相關問題