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="org" class="form-control js-example-basic-single" placeholder="请选择机构">
<option th:each="orgObj : ${orgs}" th:value="${orgObj.orgId}"
th:text="${orgObj.orgName}"
xmlns:th="http://www.w3.org/1999/xhtml"></option>
</select>
<input type="hidden" value="" name="orgId" id="orgId"/>
<input type="hidden" value="" name="orgName" id="orgName"/>
</div> 
//js代码
$("#org").select2({
placeholder:'请选择机构',
allowClear:true
});
$("#org").val(null).trigger("change");
$("#org").on("change",function () {
var seled = $(this).val();
$("#orgId").val(seled);
var orgName = $("#org option:selected").text();
$("#orgName").val(orgName);
}); 

 另外我还找了别的,都行得通的:

//other
$("#org option:selected").val()取值
$("#org option:selected").text()取文本

$("#org").find("option:selected").val()//取值
$("#org").find("option:selected").text()//取文本

var item = $("#org").selectedIndex//获取选中项的索引
$("#org").options[item].val();//取值
$("#org").options[item].text();//取文本

//其他获取选中项索引的方法
var item = $("#org").get(0).selectedindex;//item为索引值
$('#org').prop('selectedIndex');
$('option:selected', '#org').index();
$('#org option').index($('#org option:selected')) 

猜你喜欢

转载自www.cnblogs.com/xuchao0506/p/9968243.html