zoukankan      html  css  js  c++  java
  • 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')) 
  • 相关阅读:
    HDU 4472 Count DP题
    HDU 1878 欧拉回路 图论
    CSUST 1503 ZZ买衣服
    HDU 2085 核反应堆
    HDU 1029 Ignatius and the Princess IV
    UVa 11462 Age Sort
    UVa 11384
    UVa 11210
    LA 3401
    解决学一会儿累了的问题
  • 原文地址:https://www.cnblogs.com/xuchao0506/p/9968243.html
Copyright © 2011-2022 走看看