简体   繁体   English

jQuery根据数据值进行选择

[英]jQuery Selecting based on a data value

I have several input elements, that only differ on their data- attribute. 我有几个输入元素,只是它们的data-attribute不同。

<input name="country" data-action="buy"/>
<input name="country" data-action="sell"/>

I'm trying to get the value that the user entered into the input with a "buy" data- 我试图获取用户使用“购买”数据输入到输入中的值,

What's the selector I should use? 我应该使用什么选择器?

$("input[name=country] ????").val()

(There is a reason they have the same name and no id that's not related to the question) (有一个原因是他们具有相同的名称,并且没有与问题无关的ID)

只需将data-action="buy"添加为属性选择器

$('input[name="country"][data-action="buy"]').val()
$("input[data-action='buy']").val()
$("input[name='country']").filter(i) {
  return $(this).data('action') == 'buy';
}).val();

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

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