用select2设置指定值为选中状态并显示

用select2设置指定值为选中状态并显示
$("#select2_Id").val("XXX").select2();
或者;
var obj= $("#table2_Id").select2();
obj.val("XXX").trigger("change");

例子:

<label class="col-sm-2 control-label red">户口类别:<span class="help-inline"><font color="red">*</font> </span></label>
<div class="col-sm-4">
	<form:select path="properties"  class="form-control required autochange">
		<form:options items="${fns:getDictList('properties')}" itemLabel="label" itemValue="value" htmlEscape="false" />
	</form:select>
</div>


 <label class="col-sm-2 control-label red ">证件号码:</label>
<div class="col-sm-4" id="code">
	<form:input path="cardcode" id="codeId" htmlEscape="false" maxlength="18" placeholder="请输入证件号码"
		class="form-control " onblur="getBirthdayFromIdCard('2')"/>
</div>
			
$(function(){
	// 根据身份证号前三位自动判断选中户口类型
     $("#codeId").change(function(){
    var codeIdPreThree = $(this).val().substr(0,3);
   if (codeIdPreThree == "310") {
	   // 设置值为1的option为选中状态
	   $(".autochange").val("1").trigger("change");
   }else{
	   $(".autochange").val("7").trigger("change");
   }
})

转发请标明出处:【https://www.cnblogs.com/cgy-home/p/11208963.html】谢谢!

posted @ 2019-07-18 17:50  白玉神驹  阅读(8008)  评论(0编辑  收藏  举报