简体   繁体   English

单击按钮即可从最接近的所选下拉选项中获取文本

[英]Get text from closest selected dropdown option with class on button click

I have many dropdowns, when I click the button I want it to look up the select option that is selected and alert me the text it says, like 'grape'. 我有很多下拉菜单,当我单击按钮时,我希望它查找所选的选择选项,并提醒我说“ grape”的文字。

It must use .closest, have tried many variations but cant seem to find it..... 它必须使用.closest,尝试了许多变体,但似乎找不到它.....

<select name="div0" class="dropdowns">
  <option value="orange">orange</option>
  <option value="apple">apple</option>
  <option value="grape">grape</option>
  <option value="peas">peas</option>
</select>

<input type="button" name="mybutton" value="Edit Row">

<select name="div1" class="dropdowns">
  <option value="orange">orange</option>
  <option value="apple">apple</option>
  <option value="grape">grape</option>
  <option value="peas">peas</option>
</select>

<input type="button" name="mybutton" value="Edit Row" class="editrowbutton">

JQUERY (Nearest I can get)
$(document).on('click', '.editrowbutton', function() {

     var thetext =  $(this).closest('select').find('option:selected').text();

    alert(thetext);
});

You are missing the class editrowbutton on your buttons in the example. 在示例中,您的按钮上缺少类editrowbutton。

use .prev to get the previous element from target. 使用.prev从目标获取上一个元素。

$(document).on('click', '.editrowbutton', function() {
    var thetext =  $(this).prev().find('option:selected').text();
    alert(thetext);
});

http://jsfiddle.net/SeanWessell/wt00c2wu/ http://jsfiddle.net/SeanWessell/wt00c2wu/

.closest looks up the tree at parents. .closest看着父母的树。 Since the select is not a parent you will not return anything. 由于选择不是父项,因此您将不会返回任何内容。

If you wanted to search the siblings you can use prev('selector') or next('selector') as well. 如果要搜索同级,也可以使用prev('selector')next('selector')

https://api.jquery.com/category/traversing/tree-traversal/ https://api.jquery.com/category/traversing/tree-traversal/

You can also use the data-* attribute, preventing DOM position mistakes. 您也可以使用data- *属性,以防止DOM位置错误。

 $(function(){ $(document).on("click",'.editrowbutton',function(){ var target = $(this).data("select"); alert($('select[name='+target+'] option:selected').text()) }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select name="div0" class="dropdowns"> <option value="orange">orange</option> <option value="apple">apple</option> <option value="grape">grape</option> <option value="peas">peas</option> </select> <input type="button" name="mybutton" data-select="div0" class="editrowbutton" value="Edit Row"> <select name="div1" class="dropdowns"> <option value="orange">orange</option> <option value="apple">apple</option> <option value="grape">grape</option> <option value="peas">peas</option> </select> <input type="button" name="mybutton" data-select="div1" class="editrowbutton" value="Edit Row"> 

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

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