最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - select an input by value? - Stack Overflow

programmeradmin0浏览0评论

I have a bunch of hidden inputs on a page...

<input type='hidden' name='thing' value='' />;
<input type='hidden' name='thing' value='' />;
<input type='hidden' name='thing' value='' />;

Etc...

Each input can have an arbitrary value.

In jquery, what is the best way to check if one of these inputs has been set to a known, specific value?

Thanks

I have a bunch of hidden inputs on a page...

<input type='hidden' name='thing' value='' />;
<input type='hidden' name='thing' value='' />;
<input type='hidden' name='thing' value='' />;

Etc...

Each input can have an arbitrary value.

In jquery, what is the best way to check if one of these inputs has been set to a known, specific value?

Thanks

Share Improve this question edited Sep 24, 2015 at 7:26 moffeltje 4,6698 gold badges39 silver badges63 bronze badges asked Feb 22, 2011 at 21:49 travtrav 111 silver badge2 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

If you know that the value attribute is set to a specific value you can use the attribute selector

$('input[value="something"]');

http://api.jquery./category/selectors/

Edit to add:

you may want to chain the attr selector as input[name="thing"][value="something"] and @Drackir is right, you can test if it matches by whether one or more elements is in the set via the length property of the match.

$('input[type="hidden"][value="5084405"]');

hope it helps

The following is what I'd suggest. You want to loop through all of the items with the name and check them and do something for each matching one.

$('input[name="thing"]').each(function() {
      var itemValue = this.val();
      if(itemValue == "X" || itemValue == "Y")
        alert("Item value is " + itemValue);
    });
发布评论

评论列表(0)

  1. 暂无评论