如何设置无线电选项检查onload与jQuery?
需要检查是否没有设置默认值,然后设置默认值
如何设置无线电选项检查onload与jQuery?
需要检查是否没有设置默认值,然后设置默认值
当前回答
我想你可以假设,这个名称是唯一的,组中的所有电台都有相同的名称。然后你可以像这样使用jQuery支持:
$("[name=gender]").val(["Male"]);
注意:传递数组很重要。
的版本:
if (!$("[name=gender]:checked").length) {
$("[name=gender]").val(["Male"]);
}
其他回答
如果你想从你的模型中传递一个值,并且想要根据值从加载组中选择一个单选按钮,那么使用:
Jquery:
var priority = Model.Priority; //coming for razor model in this case
var allInputIds = "#slider-vertical-" + itemIndex + " fieldset input";
$(allInputIds).val([priority]); //Select at start up
和html:
<div id="@("slider-vertical-"+Model.Id)">
<fieldset data-role="controlgroup" data-type="horizontal" data-mini="true">
<input type="radio" name="@("radio-choice-b-"+Model.Id)" id="@("high-"+Model.Id)" value="1" checked="checked">
<label for="@("high-"+Model.Id)" style="width:100px">@UIStrings.PriorityHighText</label>
<input type="radio" name="@("radio-choice-b-"+Model.Id)" id="@("medium-"+Model.Id)" value="2">
<label for="@("medium-"+Model.Id)" style="width:100px">@UIStrings.PriorityMediumText</label>
<input type="radio" name="@("radio-choice-b-"+Model.Id)" id="@("low-"+Model.Id)" value="3">
<label for="@("low-"+Model.Id)" style="width:100px">@UIStrings.PriorityLowText</label>
</fieldset>
</div>
我想你可以假设,这个名称是唯一的,组中的所有电台都有相同的名称。然后你可以像这样使用jQuery支持:
$("[name=gender]").val(["Male"]);
注意:传递数组很重要。
的版本:
if (!$("[name=gender]:checked").length) {
$("[name=gender]").val(["Male"]);
}
这适用于多个单选按钮
$('input:radio[name="Aspirant.Gender"][value='+jsonData.Gender+']').prop('checked', true);
//如果你在javascript或者像backbone这样的框架上做,你会遇到很多这种情况 可以是这样的
$MobileRadio = $( '#mobileUrlRadio' );
而
$MobileRadio.checked = true;
不会起作用,
$MobileRadio[0].checked = true;
会的。
你的选择器也可以像上面推荐的那样。
下面是使用上述方法的示例:
<div class="ui-field-contain">
<fieldset data-role="controlgroup" data-type="horizontal"> <legend>Choose a pet:</legend>
<input type="radio" name="radio-choice-2" id="radio-choice-1" value="choice1">
<label for="radio-choice-1">Cat</label>
<input type="radio" name="radio-choice-2" id="radio-choice-2" value="choice2">
<label for="radio-choice-2">Dog</label>
<input type="radio" name="radio-choice-2" id="radio-choice-3" value="choice3">
<label for="radio-choice-3">Hamster</label>
<input type="radio" name="radio-choice-2" id="radio-choice-4" value="choice4">
<label for="radio-choice-4">Lizard</label>
</fieldset>
</div>
在javascript中:
$("[name = 'radio-choice-2'][value='choice3']").prop('checked', true).checkboxradio('refresh');