var sGender = value.UsrMasGender; //(Return Value )
if(sGender==="Male"){ //Set ti variable
$('#rdGender').find(':radio[name=rdGender][value="Male"]').prop('checked', true); // #rdGender is name of the RB
$('#rdGender').find(':radio[name=rdGender][value="Fe-Male"]').prop('checked', false);
}else{
$('#rdGender').find(':radio[name=rdGender][value="Fe-Male"]').prop('checked', true);
$('#rdGender').find(':radio[name=rdGender][value="Male"]').prop('checked', false);
alert("Gender : "+sGender);)
}
//返回值,但按钮未根据功能设置
<div class = "form-group">
<label for = "UserMas" class = "col-sm-2 control-label">Gender</label>
<div class = "col-sm-10">
<input type="radio" name="rdGender" id="Male" value="Male" > Male
<input type="radio" name="rdGender" id="FeMale" value="Fe-Male"> Fe-male
</div>
</div>
答案 0 :(得分:0)
尝试attr而不是道具。 i.e btn.attr("checked", true);
答案 1 :(得分:0)
试试这个:
$("input[name=rdGender][value=Male]").prop('checked', true);
OR
$("input[name=rdGender][value=FeMale]").prop('checked', true);
答案 2 :(得分:0)
尝试此操作:使用课程form-group
查找单选按钮。
var sGender = "Male"; //(Return Value )
if(sGender==="Male"){
$('.form-group').find(':radio[name=rdGender][value="Male"]').prop('checked', true); // #rdGender is name of the RB
$('.form-group').find(':radio[name=rdGender][value="Fe-Male"]').prop('checked', false);
}else{
$('.form-group').find(':radio[name=rdGender][value="Fe-Male"]').prop('checked', true);
$('.form-group').find(':radio[name=rdGender][value="Male"]').prop('checked', false);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class = "form-group"> <label for = "UserMas" class = "col-sm-2 control-label">Gender</label> <div class = "col-sm-10"> <input type="radio" name="rdGender" id="Male" value="Male" > Male <input type="radio" name="rdGender" id="FeMale" value="Fe-Male"> Fe-male</td> </div> </div>
答案 3 :(得分:0)
以下代码选择单选按钮,然后过滤以保留具有匹配值的单选按钮,然后将checked
属性设置为true
:
$(':radio[name="rdGender"]').filter(function() {
return this.value == sGender;
})
.prop( 'checked', true );
或者,假设您的ID是唯一的:
$('#Male').prop( 'checked', sGender == 'Male' );
$('#FeMale').prop( 'checked', sGender == 'Fe-Male' );