简体   繁体   English

我如何找到使用 Jquery 选择该选项的文本值

[英]how can i find use the Jquery select that seleted option textvalue

I want to find select tag's textvalue to use node .:) I made that div container is addable.我想找到 select 标签的 textvalue 来使用 node 。:) 我让那个 div 容器是可添加的。 that contains <span> and <select>包含<span><select>

<div class="selector" style="width: 140px;">

    <span style="width: 128.4px; -webkit-user-select: none;">::before</span>

          <select id="category0" name="category" class="category">

            <option selected="selected" value="0">Select a category</option>

            <option value="0">All</option>

            <option value="01">TOMATO</option>

            <option value="02">APPLE</option>

            <option value="03">BANANA</option>

        </select>

</div>

All <div> containers name is category.所有<div>容器名称都是类别。 I want to add value that selected <option> tags text.(not value ex)BANANA)我想添加选定<option>标记文本的值。(不是值 ex)BANANA)

$(document).on("change","[name='category']",function(){

  //$(this) means <select>tag

    $(this).prev().html( "I want to contain selected options text" );

}); 

I tried this and failed...:(...我试过了,但失败了...:(...

       $(this).children().each(function(index,item){

            if(item.attr("value")==findval){

            testop=item.text();

            }

            });

        $(this).prev().html(testop);

Use .find('option:selected') to find the selected option element.使用.find('option:selected')查找选定的option元素。 .text() will return the textContent of the selected element. .text()将返回所选元素的textContent [ Ref ] [参考]

 $(document).on("change", "[name='category']", function() { var textOfOption = $(this).find('option:selected').text(); $(this).prev().html(textOfOption); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <span style="width: 128.4px; -webkit-user-select: none;">::before</span> <select id="category0" name="category" class="category"> <option selected="selected" value="0">Select a category</option> <option value="0">All</option> <option value="01">TOMATO</option> <option value="02">APPLE</option> <option value="03">BANANA</option> </select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM