簡體   English   中英

jquery mobile設置單選按鈕組的選定值

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM