我在项目中是这样实现的,设置一个会话框,将自己需要添加的自定义内容输入至会话框内,然后获取会话框输入的内容,添加至select选项,然后选中,这样就可以间接实现设置自定义值为select选中项。
$("#dialog_bdz").dialog({
autoOpen: false,
modal: true,
position: { my: "center", at: "left+650px top+40px ", of: window },
buttons: {
"确定": function () {
var input_byhand = document.getElementById("input_bdz").value;
$("#Bdz_select").append("<option value='" + input_byhand + "'>" + input_byhand + "</option>");
$("#Bdz_select").val("" + input_byhand + "");
$(this).dialog("close");
},
"取消": function () {
$(this).dialog("close");
}
}
});
<div class="col-sm-6" id="Pdz_div" style="display:none">
<div class="col-sm-8">
<select class="form-control select2" style="width: 100%;" id="Pdz_select">
<option value="">请选择配电站</option>
</select>
</div>
<div class="col-sm-4">
<button type="button" class="btn btn-primary" value="pdz" οnclick="Enter_byhand(this.value)">手动输入</button>
</div>
</div>
</div>