循环遍历任何给定formId中的每个输入

时间:2018-04-29 09:40:24

标签: javascript html ajax

如何通过表单id

循环遍历任何表单的每个输入
$("form#add-librarian-form :input[type!='button']").each(function () {
    var input = $(this);
    var formGroup = input.parents('.form-group');

    if (isEmptyInput(input)) {
        removeSuccessClasses(formGroup);
        removeErrorMessage(formGroup);
        addErrorClasses(formGroup);
        validForm = false;
    } else {
        validForm = validForm & validateInput(input, formGroup);
    }
});
return validForm;

此声明 $(“form”+ formId +“:输入[type!='button']”)。每个都不起作用!!

1 个答案:

答案 0 :(得分:0)

有更多合法的方式来迭代表单元素(抱歉为vanilla JS :))

var form = document.getElementById("yourAwesomeForm")
for (var i = 0; i < form.elements.length; i++) {
    var input = form.elements[i]

    // this is unnecessary, if you want all form elements to be processed
    if (input.getAttribute("name")) {
        /* do something with input */
    }
}