开发者

Help with jQuery selector [duplicate]

开发者 https://www.devze.com 2023-03-11 01:44 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Check for values in multidimensional input array
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Check for values in multidimensional input array

I'm using a jQuery selector to look for duplicate tokens in a set of input tags that looks like this:

<ul id="class-items">
 <li>
  <input type="hidden" name="tokens[0][Search_Type]" value="a" />
  <input type="hidden" name="tokens[0][Search_Term]" value="123" />
 </li>
 <li>
  <input type="hidden" name="tokens[1][Search_Type]" value="b" />
  <input type="hidden" name="tokens[1][Search_Term]" value="456" />
 </li>
&开发者_开发知识库lt;/ul>

This is my jQuery selector check for duplicate tokens, which doesn't seem to be working:

if ($('#class-items > li > input[name$="Search_Type"][value="' + searchType + '"] + input[name$="Search_Term"][value="' + searchTerm + '"]').length == 0)

I know that the hidden tokens are adding correctly as I can see from viewing the DOM source.


Your name attributes do not end with Search_Type and Search_Term.

They have a ] at the end, so the selector should use Search_Type] and Search_Term], as in:

     // -------------------------------------v
'#class-items > li > input[name$="Search_Type]"][value="' + 
  searchType + '"] + input[name$="Search_Term]"][value="' + 
  searchTerm + '"]'


Have you tried giving the inputs simpler names (e.g. input1..) to make it's not that that's causing your problems?


Consider splitting up your selector to be sure that the + operator correctly matches elements at the same level:

$('#class-items > li').find('input[name$="Search_Type]"][value="' + searchType + '"] +
                             input[name$="Search_Term]"][value="' + searchTerm + '"]')

NB: per someone else's comment to your question, also your name$= terms are incorrect because they omitted the trailing square bracket.

0

精彩评论

暂无评论...
验证码 换一张
取 消