[英]jquery mobile set selected value of a radio button group
I have a radio button group like this: 我有一个这样的单选按钮组:
<div data-role="fieldcontainer">
<fieldset data-role="controlgroup" data-type="horizontal" name="optRestriction" id="optRestriction">
<legend>Restriction</legend>
<input type="radio" name="chkRestriction" id="chkRed" value="R" class="custom" />
<label for="chkRed">Red</label>
<input type="radio" name="chkRestriction" id="chkYello" value="Y" class="custom" />
<label for="chkYello">Yellow</label>
<input type="radio" name="chkRestriction" id="chkGreen" value="G" class="custom" />
<label for="chkGreen">
I am trying to set the selected value after retrieving values from the serve API. 我试图从服务API检索值后设置选定的值。
I have tried various ways like below: 我尝试了各种方法,如下所示:
$("input[name=chkRestriction][value=" + data.rows[0].restrictionCd + "]").prop('checked', true).trigger('change');
$("input[type='radio']:eq(" + data.rows[0].restrictionCd + ")").attr("checked", "checked");
$("input[type='radio']").checkboxradio("refresh");
$("input[name=chkRestriction][value=" + data.rows[0].restrictionCd + "]").prop('checked', true).trigger('change');
$('[name="chkRestriction"]').val([ data.rows[0].restrictionCd ]);
But none seem to work. 但似乎都没有效果。 A demo fiddle is here
这里有一个演示小提琴
Appreciate any suggestions in advance. 提前感谢任何建议。
Set attribute "checked" and call refresh. 设置属性“已选中”并调用刷新。
$('input:radio[name="chkRestriction"]').filter('[value="R"]').attr("checked",true).checkboxradio("refresh");
Jsfiddle - https://jsfiddle.net/of7uvbwh/3/ Jsfiddle - https://jsfiddle.net/of7uvbwh/3/
Set/unset the value of each radio button in a loop like this this: 设置/取消设置循环中每个单选按钮的值,如下所示:
var valToSet = myVal; // myVal is value to set from API
$('#optRestiction input').each(function(){
var $this = $(this)
if($this.val() == valToSet) {
$this.prop('checked', true);
}
else {
$this.prop('checked', false);
}
});
The 'changed' event fires when one of your inputs changes state. 当您的某个输入改变状态时,“已更改”事件将触发。 Triggering it will accomplish nothing unless you have defined a handler for the 'changed' event for that input.
除非您为该输入定义了“已更改”事件的处理程序,否则触发它将无法完成任何操作。
Try this. 尝试这个。
$('input:radio[name="chkRestriction"]').filter('[value="R"]').attr("checked",true).checkboxradio().checkboxradio("refresh"); $( '输入:无线电[名称= “chkRestriction”]')滤波器( '[=值 “R”')ATTR( “选中”,真).checkboxradio()checkboxradio( “刷新”);。。。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.