简体   繁体   English

检查多维输入数组中的值

[英]Check for values in multidimensional input array

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?如何快速检查是否存在Search_Term = X 和Search_Type = Y 的token If there's a way to do it in one jquery line rather than in a loop that'd be awesome.如果有办法在一条 jquery 行中而不是在一个循环中做到这一点,那就太棒了。

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/示例: http://jsfiddle.net/niklasvh/56jLf/

But whether or not that is more efficient than with looping, I don't know.但是这是否比循环更有效,我不知道。

Jquery: Jquery:

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

If you are checking the submitted array as a javascript object, you can use jsonpath to perform xpath like queries on a dataset.如果您将提交的数组检查为 javascript object,则可以使用jsonpath对数据集执行 xpath 之类的查询。

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

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