开发者

Check for values in multidimensional input array

开发者 https://www.devze.com 2023-03-10 16:55 出处:网络
I store values in a multi-dimensional hidden input array that looks like this: <input type=\"hidden\" name=\"tokens[0][Search_Type]\" value=\"a\" />

I store values in a multi-dimensional hidden input array that looks like this:

<input type="hidden" name="tokens[0][Search_Type]" value="a" />
<input type="hidden" name="tokens[0][Search_Term]" value="123" />
<input type="hidden" name="tokens[1][Search_Type]" value="b" />
<input type="hidden" name="tokens[1][Search_Term]" value="456" />

How can I quickly check whether there is a token with Search_Term = X and Search_Type = Y? If there's a way to do it in one jquery line rather than开发者_如何学Python in a loop that'd be awesome.


Jquery:

token_found = $('input[name$=Search_Type]][value=Y]
                 +
                 input[name$=Search_Term]][value=X]'
               ).length > 0;


You could do it with selectors as well:

// found
console.log($('input[name$="[Search_Type]"][value="a"]').next('input[name$="[Search_Term]"][value="123"]').length); 

// not found
console.log($('input[name$="[Search_Type]"][value="b"]').next('input[name$="[Search_Term]"][value="123"]').length); 

// found
console.log($('input[name$="[Search_Type]"][value="b"]').next('input[name$="[Search_Term]"][value="456"]').length); 

Example: http://jsfiddle.net/niklasvh/56jLf/

But whether or not that is more efficient than with looping, I don't know.


If you are checking the submitted array as a javascript object, you can use jsonpath to perform xpath like queries on a dataset.

0

精彩评论

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