簡單描述:后台需要獲取到select標簽選擇的內容,也就是text,該怎么取呢?很簡單。
代碼:
//hml代碼
<div class="col-md-6">
<label class="control-label flex" style="margin-top: 10px;">
機構<span class="star align-items">*</span>
</label>
<select id="orgnize" class="form-control js-example-basic-single" placeholder="請選擇機構">
<option th:each="orgnizeObj : ${orgnizes}" th:value="${orgnizeObj.orgnizeId}"
th:text="${orgnizeObj.orgnizeName}"
xmlns:th="http://www.w3.org/1999/xhtml"></option>
</select>
<input type="hidden" value="" name="orgnizeId" id="orgnizeId"/>
<input type="hidden" value="" name="orgnizeName" id="orgnizeName"/>
</div>
//js代碼
$("#orgnize").select2({
placeholder:'請選擇機構',
allowClear:true
});
$("#orgnize").val(null).trigger("change");
$("#orgnize").on("change",function () {
var seled = $(this).val();
$("#orgnizeId").val(seled);
var orgnizeName = $("#orgnize option:selected").text();
$("#orgnizeName").val(orgnizeName);
});
另外我還找了別的,都行得通的:
//other
$("#orgnize option:selected").val()取值
$("#orgnize option:selected").text()取文本
$("#orgnize").find("option:selected").val()//取值
$("#orgnize").find("option:selected").text()//取文本
var item = $("#orgnize").selectedIndex//獲取選中項的索引
$("#orgnize").options[item].val();//取值
$("#orgnize").options[item].text();//取文本
//其他獲取選中項索引的方法
var item = $("#orgnize").get(0).selectedindex;//item為索引值
$('#orgnize').prop('selectedIndex');
$('option:selected', '#orgnize').index();
$('#orgnize option').index($('#orgnize option:selected'))
