个人代码1:
<div class="form-group" style="width: 250px;margin:0 auto;">
<label for="">性别</label>
<input type="radio" id="driverSex" name="driverSex" value="男"/>男
<input type="radio" id="driverSex" name="driverSex" value="女"/>女
</div>
注意: 1.value属性不要写错,写错或没写会获取的值默认是on
2.name 属性必须一致
个人代码2:
//编辑
function editdriver(id) {
$.ajax({
url:"${pageContext.request.contextPath}/upadatedriver",
type:"GET",
data:{"id":id},
success:function(result){
console.log(result);
$("input[name=driverSex]").val([result.driverSex]);//回显性别
}
});
$("#driver").modal("show");//打开模态框
}
其他参考:
1.获取选中值,三种方法都可以:
$(\'input:radio:checked\').val();
$("input[type=\'radio\']:checked").val();
$("input[name=\'rd\']:checked").val();
2.设置第一个Radio为选中值:
$(\'input:radio:first\').attr(\'checked\', \'checked\');
或者
$(\'input:radio:first\').attr(\'checked\', \'true\');
注:attr("checked",\'checked\')= attr("checked", \'true\')= attr("checked", true)
3.设置最后一个Radio为选中值:
$(\'input:radio:last\').attr(\'checked\', \'checked\');
或者
$(\'input:radio:last\').attr(\'checked\', \'true\');
4.根据索引值设置任意一个radio为选中值:
$(\'input:radio\').eq(索引值).attr(\'checked\', \'true\');索引值=0,1,2....