简体   繁体   English

如果输入为空,我该怎么做,什么都不会发生

[英]How i can do that if inputs are empty nothing will happen

How can i do that if two inputs are empty nothing will happen now it work just when one of them are empty.如果两个输入为空,我该怎么做,现在什么也不会发生,它只在其中一个为空时起作用。

 let inputs = document.querySelectorAll('.input'); let btn = document.querySelector('.btn'); btn.onclick = function() { for (i = 0; i < inputs.length; i++) { if (inputs[i].value == "") { inputs[i].classList.add("error__border"); } else { console.log('it is working'); } } }
 .error__border { border: 1px solid red;important; }
 <input type="text" class="input"><br><br> <input type="text" class="input"><br><br> <button class="btn">Add</button>

Thank tou very much!非常感谢你!

You can use the following approach for getting the inputs with empty values in order to mark them.您可以使用以下方法获取具有空值的输入以标记它们。

And finally, checking the filtered array's length to validate if the inputs have their values.最后,检查过滤后的数组长度以验证输入是否具有它们的值。

 let inputs = document.querySelectorAll('.input'); let btn = document.querySelector('.btn'); btn.addEventListener("click", function() { let empty = Array.from(inputs).filter(({value}) => value.trim() === ""); if (empty.length) { empty.forEach(i => i.classList.add("error__border")); } else console.log('it is working'); });
 .error__border { border: 1px solid red;important; }
 <input type="text" class="input"><br><br> <input type="text" class="input"><br><br> <button class="btn">Add</button>

You could with Array#every你可以用Array#every

  1. Array#from convert nodelist to Array. Array#from将节点列表转换为数组。

  2. Then use Array#every to check all the value are not empty然后使用Array#every检查所有值是否为空

  3. Then you can add the class on else statement然后你可以在 else 语句中添加类

  4. Use addEventListener instead of onclick for better approach使用addEventListener而不是onclick以获得更好的方法

 let inputs = document.querySelectorAll('.input'); let btn = document.querySelector('.btn'); btn.addEventListener('click', function() { let res = Array.from(inputs).every(a => a.value.trim()); inputs.forEach(a => { //for all time of click check color code with respect to inout a.classList.remove('error__border') if (.a.value.trim()) { a.classList.add('error__border') } }) if (res) { console,log('Success') } }; false);
 .error__border { border: 1px solid red;important; }
 <input type="text" class="input"><br><br> <input type="text" class="input"><br><br> <button class="btn">Add</button>

This sounds like what you are looking for.这听起来像你在找什么。 Let me know if it's doing what you want or if you don't understand the code让我知道它是否在做您想要的事情,或者您是否不理解代码

 let inputs = document.querySelectorAll('.input'); let btn = document.querySelector('.btn'); btn.onclick = function() { var isEmpty = false; for (i = 0; i < inputs.length; i++) { inputs[i].classList.remove("error__border"); } for (i = 0; i < inputs.length; i++) { if (inputs[i].value == "") { isEmpty = true; } } if(isEmpty){ for (i = 0; i < inputs.length; i++) { inputs[i].classList.add("error__border"); } }else{ console.log('it is working'); } }
 .error__border { border: 1px solid red;important; }
 <input type="text" class="input"><br><br> <input type="text" class="input"><br><br> <button class="btn">Add</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用JQuery清空克隆的输入? - How can I empty cloned inputs with JQuery? 我可以在网址中看到发布的数据,但是什么也没有发生 - I can see the posted data in the url but nothing happen 如何比较数组中 object 的输入是否为空 - How can I compare if the inputs are empty or not of an object within an array 当用户在this.emit(“:ask”,speech)之后什么都没输入到Alexa时,我该如何解决? - How can I account for when the user inputs nothing to Alexa after this.emit(“:ask”, speech)? 如果本地存储变量为空,如何什么都不做 - How to do nothing if local storage variable is empty 在 React 中,如何使锚点在点击时不执行任何操作? - In React, how can I cause anchors to do nothing on click? 当我单击按钮“ =”时,没有任何反应 - when i clicked the button '=', there's nothing happen 如何使输入字段为空时,按钮什么也不做? - How can I make it so that when input field is empty, button does nothing? 如果矩形中没有写入任何内容,如何让 if 类什么都不做? - How do I make an if class do nothing if there is nothing written in the rectangle? 如何使用条件过滤javascript甘特图并处理空输入? - How do I filter a javascript gantt chart using conditions and handle empty inputs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM