jquery 移动设置单选按钮组的选中值

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 ]);

但 none 似乎有效。演示 fiddle is here

提前感谢任何建议。

Set/unset 循环中每个单选按钮的值,如下所示:

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);
  }
});

'changed' 事件在您的输入之一更改状态时触发。除非您为该输入的 'changed' 事件定义了处理程序,否则触发它不会有任何效果。

设置属性 "checked" 并调用刷新。

 $('input:radio[name="chkRestriction"]').filter('[value="R"]').attr("checked",true).checkboxradio("refresh");

Jsfiddle - https://jsfiddle.net/of7uvbwh/3/

试试这个。

$('input:radio[name="chkRestriction"]').filter('[value="R"]').attr("checked",true).checkboxradio().checkboxradio("refresh");