2016-01-20 37 views
0

我有一頁輸入字段用於數據輸入。沒有輸入字段應該有重複。jQuery如果輸入值存在於任何其他輸入字段然後做點什麼

如果用戶在輸入字段中輸入「ABC」並且標出該功能應該檢測在任何其他輸入字段中是否存在「ABC」。如果該值存在,則會在每個輸入字段(包括當前輸入字段)之後顯示錯誤消息。

如果在任何這些輸入字段中值被更改(例如爲「DBE」),並且因此沒有重複,則應隱藏所有消息。

我目前堅持以下幾點:

$("input[type='text']").on("keyup change", function() { 
    var value = this.value; 

    if ($("input[type='text']:contains('" + value"')").length > 1) 
       { 
        $(this).find(".error").show(); 
       } 
     else { 
        $(this).find(".error").hide(); 
       } 
     }  
});   
+2

你的花括號不匹配 – Coderchu

+1

你也應該告訴人們什麼你就「卡殼」。就像你遇到了一些不想要的行爲,頁面崩潰等等。 – Coderchu

回答

1

你在你的代碼中的額外的右括號。此外,您還需要另一個+跡象加入字符串value

$("input[type='text']").on("keyup change", function() { 
    var value = this.value; 

    // added another '+' sign here ---------------| 
    if ($("input[type='text']:contains('" + value + "')").length > 1) { 
     $(this).find(".error").show(); 
    } 
    else { 
     $(this).find(".error").hide(); 
    } 
    // } <--- remove this. 
});  
+0

感謝@Shijin指出其中一個編輯中的加號。 – Coderchu

相關問題