【发布时间】:2014-03-29 03:10:35
【问题描述】:
假设我有一组像这样的 3 个单选按钮:
<label for="a-1">A</label>
<input type="radio" name="a" id="a-1" value="1" />
<label for="a-2">A</label>
<input type="radio" name="a" id="a-2" value="2" />
<label for="a-3">A</label>
<input type="radio" name="a" id="a-3" value="3" />
第一个收音机设置为checked="true"ala
$("input").eq(0).attr("checked", true);
JQM API for checkboxradio 表示如下:
$( ".selector" ).prop( "checked", true ).checkboxradio( "refresh" );
以编程方式设置单选按钮。
问题:
如果我需要将收音机从一个按钮切换到另一个按钮怎么办?所以....
$("input").eq(1).trigger("click");
是否总是需要在我的点击处理程序上单独更新我的单选按钮,还是不应该开箱即用?毕竟,当我有一个非 JQM 单选按钮组并触发单击单选元素时,之前选中的按钮会自动取消选中。
谢谢!
编辑:
简单的例子:
var foo = '<input type="radio" checked="checked" name="a" id="a-1" value="1" />' +
'<input type="radio" name="a" id="a-2" value="2" />' +
'<input type="radio" name="a" id="a-3" value="3" />';
$("div.ui-content").append( $(foo) );
$(document.getElementById("a-2")).trigger("click");
这会生成三个单选按钮,第一个被选中。点击后,第二个收音机被选中而第一个未被选中。
编辑:
JQM 示例:
$("div.ui-content").empty()
var bar = '<div data-role="controlgroup">' +
'<input type="radio" name="a" id="a-1" value="choice-1" checked="checked">' +
'<label for="a-1">1</label>' +
'<input type="radio" name="a" id="a-2" value="choice-2">' +
'<label for="a-2">2</label>' +
'<input type="radio" name="a" id="a-3" value="choice-3">' +
'<label for="a-3">3</label>' +
'</div>';
$("div.ui-content").append( $(bar) ).enhanceWithin();
$("input").eq(2).trigger("click").checkboxradio("refresh");
这将产生 2 个选择单选按钮。
【问题讨论】:
-
您到底想做什么?动态选择单选按钮而不刷新单选组,对吧?
-
不是选择,而是触发点击。在纯 HTML 中,C 上的
trigger("click")取消选中无线电 A,而在 JQM 中则没有。见jsbin.com/ofuhaw/1202
标签: javascript jquery jquery-mobile radio-button checked