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.
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/
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");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.