2010-08-25 56 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”改变“检查”状态。