JavaScript验证多个输入框

JavaScript验证多个输入框,javascript,Javascript,我有这个HTML表单: <div class="input_fields_wrap"> <div class="form-group row"> <label class="col-sm-3 col-form-label">Items</label> <div class="col-sm-7"> <input type="text" name="items[]" cl

我有这个HTML表单:

<div class="input_fields_wrap">
    <div class="form-group row">
        <label class="col-sm-3 col-form-label">Items</label>
        <div class="col-sm-7">
            <input type="text" name="items[]" class="form-control items" id="items">
        </div>
        <div class="col-sm-2">
            <button class="add_field_button btn btn-success btn-sm float-right">Add+</button>   
        </div>                      
    </div>
</div>
添加+功能的JavaScript代码:


您可以尝试这样的方法,Query selector或all,因为forEach返回undefined,所以您可以使用

const items=[…document.querySelectorAllinput[type=text]]; items.everyle=>{ //console.logele.value 如果元素值=={ 重新需要alertItem名称; ele.focus;//关注于特定的输入 返回false; } } 项目 加+
您可以尝试这样的方法,Query selector或all,因为forEach返回undefined,所以您可以使用

const items=[…document.querySelectorAllinput[type=text]]; items.everyle=>{ //console.logele.value 如果元素值=={ 重新需要alertItem名称; ele.focus;//关注于特定的输入 返回false; } } 项目 加+
我会将输入字段更改为一个名为item的类

然后我将循环所有这些项目

var items = document.querySelectorAll('.item');
for (var i = 0; i < items.length; i++) { 
    var item = items[i];
    // validate
}

我会将输入字段更改为一个名为item的类

然后我将循环所有这些项目

var items = document.querySelectorAll('.item');
for (var i = 0; i < items.length; i++) { 
    var item = items[i];
    // validate
}

这里是Shubh的答案的一个版本,但按类查询。需要注意的是,在javascript中不能通过从函数返回来短路forEach,因此我将此解决方案改为使用for循环。有关更多信息,请阅读

让items=document.queryselectoral.items 对于let i=0;i这里是Shubh的答案的一个版本,但按类查询。需要注意的是,在javascript中不能通过从函数返回来短路forEach,因此我将此解决方案改为使用for循环。有关更多信息,请阅读

让items=document.queryselectoral.items 对于let i=0;i在您的情况下,增加id的功能是非常无用的。getElementById将为您提供第一个匹配项。在您的情况下,如果要检查多个元素,最好使用QuerySelectorAll:

document.querySelectorAll('.form-control').forEach(node => {
    if(items.value == "") {
        ...
    }
})

在您的情况下,增加id的功能是非常无用的。getElementById将为您提供第一个匹配项。在您的情况下,如果要检查多个元素,最好使用QuerySelectorAll:

document.querySelectorAll('.form-control').forEach(node => {
    if(items.value == "") {
        ...
    }
})


id应该是唯一的-使用类而不是document.querySelectorAll.form control,或者只向textfields添加一个必需的属性。id已经显示了唯一的项,如items、items1、items2等。上面的人建议您的每个输入都具有相同的类,您可以查询该类而不是按id。只需使用[required]并省略javascript即可。像上面建议的那样,您的文本字段没有类项ID应该是唯一的-使用类而不是document.querySelectorAll.form control,或者只向文本字段添加一个必需的属性。id已经显示了唯一的项,如items、items1、items2等。上面的人建议您的每个输入都具有相同的类,您可以查询该类而不是按id。只需使用[required]并省略javascript即可。像上面建议的那样,您的文本字段没有类项,我无法使用输入,因为存在其他输入字段。这就是为什么我对唯一id(如items、items1、items2等)进行了格化。请改用类,然后使用document.querySelectorAllinput[type=text]您的答案会更好。它向我显示:未捕获的语法错误:标识符“items”已被声明您可能在代码中声明了items变量两次@creativeartbdI无法使用输入,因为存在其他输入字段。这就是为什么我对唯一id(如items、items1、items2等)进行了格化。请改用类,然后使用document.querySelectorAllinput[type=text]您的答案会更好。它向我显示:未捕获的语法错误:标识符“items”已声明如果您已经有一个名为items的变量,您需要以不同的名称调用代码段中的项目。您能告诉我为什么在显示警报消息后会重新加载页面吗?哇,最后我们救了我:非常感谢。您应该在此处发布该URL,以查看foreach/nCught SyntaxError的问题:标识符“items”已被声明如果您已经有一个名为items的变量,则需要以不同的名称调用代码段中的项。您能告诉我为什么在显示警报消息后会重新加载页面吗
鼠尾草?哇,我们终于救了我:非常感谢。你应该在这里发布这个URL,让每个人都知道foreach有什么问题/
document.querySelectorAll('.form-control').forEach(node => {
    if(items.value == "") {
        ...
    }
})