select獲取選中的option(包含value和text,重點是text怎么獲取)


簡單描述:后台需要獲取到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')) 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM