select2 取值 赋值
项目中心引入了select2的插件。优势:可以多选、搜索,缺点:存取值不如select方便。
select2 取值:
<script type="text/javascript">
//取text var strtext = $('#select2').select2('data').text;
//取value
var str2value= $("#select2").select2("val");
//多选
//$('#select2').select2({ // allowClear: true, // data:[{id:0,text:'hello'},{id:1,text:'world'}] // }); </script>
<select id="select2" > </select>
Select2升级以后,支持多选了,用select2('data')取出来是是数组,不在是一个json了,
因需要select2('data')[0].text 既可以取得对应的值,多个的话循环获取
select2赋值:
页面采用razor <div class="form-group"> <label class="control-label"> 销售人员: </label> @Html.DropDownListFor(m => m.SalesMan, ViewData["CreateStaffID"] as SelectList, "请选择", new { @id = "SalesMan", @class = "select2 ", @style = "width:67%;" }) <span class="help-line">@Html.ValidationMessageFor(m => m.SalesMan)</span> </div> js: <script type="text/javascript"> //示例:给select2标签赋值23 @Html.Raw("$('#SalesMan').val("23") $("#SalesMan").select2(); }); </script>