Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jquery检查多个动态文本框中输入的值是否相同_Javascript_Jquery_Forms_Textbox - Fatal编程技术网

Javascript 使用jquery检查多个动态文本框中输入的值是否相同

Javascript 使用jquery检查多个动态文本框中输入的值是否相同,javascript,jquery,forms,textbox,Javascript,Jquery,Forms,Textbox,我有一个带有多个动态生成的文本框的表单。假设有5个具有相同名称和不同ID的文本框 我需要检查这些文本字段中输入的值是否唯一,即: 假设: <input type="text" name="textbox" id="textbox1" value="1"> <input type="text" name="textbox" id="textbox1" value="2"> <input type="text" name="textbox" id="textbox1"

我有一个带有多个动态生成的文本框的表单。假设有5个具有相同名称和不同ID的文本框

我需要检查这些文本字段中输入的值是否唯一,即:

假设:

<input type="text" name="textbox" id="textbox1" value="1">
<input type="text" name="textbox" id="textbox1" value="2">
<input type="text" name="textbox" id="textbox1" value="3">
<input type="text" name="textbox" id="textbox1" value="4">
<input type="text" name="textbox" id="textbox1" value="1">


在这种情况下,如果已经输入值,则必须显示错误。如果值不同,则只能提交表单。

我不知道jquery.validationengine,但至少可以使用

var valueX = ${'#textboxX'}.attr('value');

要使用索引读取输入的
属性
X

,我知道我迟到了,但它可以帮助其他人,因此请检查解决方案

count = Number((document.frmDefineSSS.cboTeamSize.selectedIndex) - 1);
            flagSame = false;
            for (i = 1; i <= count; i++) {
                for (j = i + 1; j <= count + 1; j++) {
                    if ($("#hidCommMemId" + [i]).val() == $("#hidCommMemId" + [j]).val()) {
                        alert("Team Member can't be same.");
                        $("#txtStudName" + [i]).css("background-color", "yellow");
                        $("#txtStudName" + [j]).css("background-color", "yellow");
                        flagSame = false;
                        return false;

                    }
                    else {
                        $("#txtStudName" + [i]).css("background-color", "white");
                        $("#txtStudName" + [j]).css("background-color", "white");
                        flagSame = true;
                    }
                }
            }
我基本上循环了所有的文本框,并在嵌套循环中将这些值与所有其他文本框进行比较。基本上,这就像是泡泡排序。我知道这很糟糕,但我想不出任何有效的方法。你们能想出更好的办法吗

function findDuplicates() {
    var isDuplicate = false;
    jQuery("input[name^='access_keys']").each(function (i,el1) {
        var current_val = jQuery(el1).val();
        if (current_val != "") {
            jQuery("input[name^='access_keys']").each(function (i,el2) {
                if (jQuery(el2).val() == current_val && jQuery(el1).attr("name") != jQuery(el2).attr("name")) {
                    isDuplicate = true;
                    jQuery(el2).css("background-color", "yellow");
                    jQuery(el1).css("background-color", "yellow");
                    return;
                }
            });
        }
    });

    if (isDuplicate) {
        alert ("Duplicate values found.");
        return false;
    } else {
        return true;
    }
}