我寫了一個基本的表單驗證腳本,現在我試圖重置在用戶沒有填寫必填字段時發生的錯誤。當一個複選框被選中時,從標籤中刪除類
對於複選框和單選按鈕,我已經添加了類error
他們的標籤。造成這種情況的HTML代碼如下所示:
<input class="required" id="Example31" name="Example3" type="checkbox" />
<label for="Example31" class="error">Example Input 3 Option 1</label>
<input class="required" id="Example32" name="Example3" type="checkbox" />
<label for="Example32" class="error">Example Input 3 Option 2</label>
<input class="required" id="Example4" name="Example4" type="radio" />
<label for="Example4" class="error">Example Input 4</label>
要添加的錯誤,我計算出,如果具有相同名稱的任何複選框被選中,使用這個腳本:
$("input.required").each(function() {
// check checkboxes and radio buttons
if ($(this).is(":checkbox") || $(this).is(":radio")) {
var inputName = $(this).attr("name");
if (!$("input[name=" + inputName + "]").is(":checked")) {
var inputId = $(this).attr("id");
$("label[for=" + inputId + "]").addClass("error");
error = true;
};
};
// end checkboxes and radio buttons
});
如何刪除錯誤而不必修改HTML?我正在畫一個完整的空白。下面是我在想什麼:
- 圖出與有錯誤
- 找到具有該ID的複選框或單選按鈕,每個標籤相關聯的名稱
- 找出複選框或單選按鈕命名
- 查找複選框或單選按鈕具有相同名稱的其餘部分
- 查找這些輸入標識
- 查找標籤與這些名字
- 克麗r是錯誤關閉這些標籤
我一件T虧損的,雖然。如果任何人都可以幫助,那將不勝感激。
你是不是想立刻從所有標籤刪除錯誤? – ATOzTOA
當你需要刪除的錯誤?當'複選框'是'未選中'? – ATOzTOA
當它成爲檢查。對不起,我一發布這個,就出去吃午飯。我會在幾分鐘內測試下面的錯誤代碼。 – JacobTheDev