dqcuq4138
2016-06-27 01:12 阅读 35

检查多个输入是否具有相同值的最佳方法

I have some inputs (random number > 0) and each is inside a div with same class so I have this structure:

<div class="myclass"> <!-- div #1 -->
    <input name="name" class="input">
    <!-- other inputs that don't take care -->
</div>
...
<div class="myclass"> <!-- div #n -->
    <input name="name" class="input">
    <!-- other inputs that don't take care -->
</div>

I need to check that the value of every input doesn't match with other, so if I have in the first input myname as value in the input n I can't have myname.

I have the next JQuery code to iterate over all divs to extract the info about every div.

$('.myclass').each(function() {
    input = $(this).find('input[name="name"]').val();
});

And then, it sends all info (JSON parsed before) through jQuery ajax to PHP scripts which MUST revalidate that input values don't be equal.

So, the question is: what's the best way to go over all divs and know if value is repeated? I thought about making a $.each inside the each I have to iterate over all elements but I don't know about its performance.

And what about with PHP script, what's the best way to afford it? Thank you.

NOTE: JSON parsed array, now have this structure:

{"user_info":{"app_id":0,"user_id":0},"contact":{"has_contact":0},"menu_items":[{"position":0,"name":"Nosotros","icon_id":"ic_delete_black_48dp.png","content":"nosotros"},{"position":1,"name":"Precios","icon_id":"ic_help_black_48dp.png","content":"nosotros"}]}

Don't worry about "user_info" and "has_contact" they are irrelevant, the name for which I was talking is the menu_items[index]['name'] value.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    douyu1656 douyu1656 2016-06-27 01:22

    You can use $.map(), .slice(), $.unique(), .length

    // return `input` values
    var inputs = $.map($(".myclass input"), function(el) {return el.value});
    // check if `unique` array `.length` is equal to `unique` array `.length`
    var unique = $.unique(inputs.slice(0)).length === inputs.length;
    
    console.log(inputs, unique);
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="myclass"> <!-- div #1 -->
        <input name="name" class="input" value="abc">
        <!-- other inputs that don't take care -->
    </div>
    ...
    <div class="myclass"> <!-- div #n -->
        <input name="name" class="input" value="abc">
        <!-- other inputs that don't take care -->
    </div>

    </div>
    
    点赞 评论 复制链接分享
  • dougua4836 dougua4836 2016-06-27 02:05

    Another way to do it (code example not shown ...) is to create a hash in which each received value (i-f it is a string or can be unambiguously coerced into one ...) is the key, and a don't-care value is the value.

    Start with an empty hash. For each input, test if a key matching that input already exists in the hash. If so, you've got a dupe. If not, add the input as a new key. (The associated value doesn't matter ...) After all inputs are checked, the hash can be discarded.

    (This strategy works for any programming language that supports hashes ... which, these days, is "all of them." It is also, in principle, an efficient way to count how many times any particular element occurs.)

    Caution: Do not use this technique where the input value is of a different data-type than that of the underlying collection-type, which in JavaScript is (only) "a string," and there is any possibility at all that two inputs which you deem to be "different" could be coerced into the same (string) key value. The algorithm will not be able to tell them apart.

    点赞 评论 复制链接分享
  • doutuoben6908 doutuoben6908 2016-06-27 03:02

    If I had to do this with PHP I would use array_unique() function....

    点赞 评论 复制链接分享

相关推荐