[英]How to set the default selected radio button in a group depending on a database value?
[英]jquery mobile set selected value of a radio button group
我有一个这样的单选按钮组:
<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">
我试图从服务API检索值后设置选定的值。
我尝试了各种方法,如下所示:
$("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 ]);
提前感谢任何建议。
设置属性“已选中”并调用刷新。
$('input:radio[name="chkRestriction"]').filter('[value="R"]').attr("checked",true).checkboxradio("refresh");
Jsfiddle - https://jsfiddle.net/of7uvbwh/3/
设置/取消设置循环中每个单选按钮的值,如下所示:
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);
}
});
当您的某个输入改变状态时,“已更改”事件将触发。 除非您为该输入定义了“已更改”事件的处理程序,否则触发它将无法完成任何操作。
尝试这个。
$( '输入:无线电[名称= “chkRestriction”]')滤波器( '[=值 “R”')ATTR( “选中”,真).checkboxradio()checkboxradio( “刷新”);。。。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.