如果选中了某个复选框,则查找有多少用户输入值是相同的

If a checkbox is checked, find how many user input values are the same

本文关键字:输入 用户 多少 查找 复选框 如果      更新时间:2023-09-26

我有一个脚本,如果复选框被选中,则显示用户输入。用户输入被设计为放置一个将其链接到其他div的值。相同的输入表示它在div的同一组中。

我怎样才能知道用户在选中复选框后输入了多少唯一值?

作为奖励,如果我能发现这些输入和复选框属于哪个主div,那就太好了。

谢谢,埃蒙


更新:

重复的粗略html将是:

<div class="master">
    <input type="checkbox">
    <input type="text">
</div>

文本输入只会在选中复选框时显示。

/* e.g.
   {
     "group a": [jQuery object with 3 master divs],
     "group b": [jQuery object with 1 master div],
     "group c": [jQuery object with 5 master divs]
   }
*/
var groups = {};
$(".master :text").each(function(i, textInput) {
    var master = $(textInput).closest(".master");
    var groupName = $(textInput).val();
    if (groups[groupName]) {
        groups[groupName] = groups[groupName].add(master);
    } else {
        groups[groupName] = master;
    }
});
// This is what you asked for
var numUniqueValues = Object.keys(groups).length;
// Now for each unique user-inputted groupName, you can access groups[groupName] to get a jQuery object of all the master divs for that group.
groups["sunshine"].css("background-color", "yellow");