一、例如:
<div class="form-group"> <label class="control-label col-lg-3">用户类型<font style="color:red;">*</font></label> <div class="col-lg-9"> <select id="UserTypeID" name="UserTypeID" class="form-control" autocomplete="off"></select> </div><!-- /.col --> </div><!-- /form-group -->
在bootstrap中会遇到这么一个问题,一个form-group里面含有一个select标签,在赋值时进行动态赋值:
function EditUser(xuhao) { $("#form_SaveUser").get(0).reset(); //清空form $("#form_SaveUser").parsley().reset(); //清空校验信息 var n = xuhao - 1; $("#form_SaveUser#UserTypeID").val(UserList[n].UserTypeID); //这里会有一个弹出口 $('#User-modal').modal('show'); if (UserList[n].UserType == "Administrator") { $("#form_SaveUser #UserTypeID").find("option[value = '管理员']").attr("selected", true); } else if (UserList[n].UserType == "General") { $("#form_SaveUser #UserTypeID").find("option[value = '普通用户']").attr("selected", true); } else if (UserList[n].UserType == "root") { $("#form_SaveUser #UserTypeID").find("option[value = '超级用户']").attr("selected", true); } else if (UserList[n].UserType == "Manager") { $("#form_SaveUser #UserTypeID").find("option[value = '负责人']").attr("selected", true); } }
在弹出时会将form表单的值清空,所以要在弹出之后将值赋上去,这样就不会受到影响。