简体   繁体   English

使用 jQuery,如何获得特定的下拉元素(<select></select> ) 基于下拉列表的值?

[英]Using jQuery, how do I get a specific dropdown element (<select></select>) based on the value of the dropdown?

I have a class of dropdowns --.selMapField -- and I want to select the dropdown that has a specific value.我有一个 class 下拉列表 --.selMapField -- 我想 select 下拉列表具有特定值。 For instance:例如:

const myattr=$(`.selMapField[value="MYSKU"]`).attr(`x`);

Use jQuery's .filter() to find the elements by value使用 jQuery 的.filter()按值查找元素

 const myattr = $(".selMapField").filter((_, { value }) => value === "MYSKU").attr("x") console.log(myattr)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.slim.min.js"></script> <select class="selMapField" x="A"><option selected>FOO</option></select> <select class="selMapField" x="B"><option selected>MYSKU</option></select> <select class="selMapField" x="C"><option selected>BAR</option></select>


As always, you might not need jQuery与往常一样,您可能不需要 jQuery

 const myattr = Array.from( document.querySelectorAll(".selMapField") ).find(({ value }) => value === "MYSKU")?.getAttribute("x") console.log(myattr)
 <select class="selMapField" x="A"><option selected>FOO</option></select> <select class="selMapField" x="B"><option selected>MYSKU</option></select> <select class="selMapField" x="C"><option selected>BAR</option></select>

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

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