首页 > 代码库 > jquery怎么根据后台传过来的值动态设置下拉框、单选框选中

jquery怎么根据后台传过来的值动态设置下拉框、单选框选中

$(function(){
var sex=$("#sex").val();
var marriageStatus=$("#marriageStatus").val();
var education=$("#education").val();
if(!isnull(sex)){
$("input:radio[name=‘sex‘][value="http://www.mamicode.com/+sex+"]").attr(‘checked‘,‘true‘);
}
if(!isnull(marriageStatus)){
$("input:radio[name=‘marriageStatus‘][value="http://www.mamicode.com/+marriageStatus+"]").attr(‘checked‘,‘true‘);
}
if(!isnull(education)){
$("#education1 option[value=http://www.mamicode.com/‘"+education+"‘]").attr("selected", true);
}
});
<input type="hidden" id="sex" value="http://www.mamicode.com/${serviceInfo.sex}"/>
<input type="radio" name="sex" value="http://www.mamicode.com/0" checked="checked"/>男
<input type="radio" name="sex" value="http://www.mamicode.com/1"/>女
<input type="hidden" id="marriageStatus" value="http://www.mamicode.com/${serviceInfo.marriageStatus}"/>
<input type="radio" name="marriageStatus" value="http://www.mamicode.com/0" checked="checked"/>是
<input type="radio" name="marriageStatus" value="http://www.mamicode.com/1"/>否
<input type="hidden" id="education" value="http://www.mamicode.com/${serviceInfo.education}"/>
<select name="education" id="education1">
<option value="http://www.mamicode.com/硕士">硕士</option>
<option value="http://www.mamicode.com/本科">本科</option>
<option value="http://www.mamicode.com/大专">大专</option>
<option value="http://www.mamicode.com/其它">其它</option>
</select>

jquery怎么根据后台传过来的值动态设置下拉框、单选框选中