javascript更改多输入背景颜色
我有大约120个输入javascript更改多输入背景颜色,javascript,background-color,Javascript,Background Color,我有大约120个输入 <input type="text" /> 我将用一些文本填充其中一些,而其他文本将是空的。 是否有任何方法可以使用“提交”按钮,使所有空输入改变背景颜色?。 任何帮助都将不胜感激。这应该可以: function verifyInputs() { var inputs = document.querySelectorAll('input'); for (var i=0; i<inputs.length; i++) {
<input type="text" />
我将用一些文本填充其中一些,而其他文本将是空的。
是否有任何方法可以使用“提交”按钮,使所有空输入改变背景颜色?。
任何帮助都将不胜感激。这应该可以:
function verifyInputs() {
var inputs = document.querySelectorAll('input');
for (var i=0; i<inputs.length; i++) {
if (inputs[i].type == 'text' && inputs[i].value == "") {
inputs[i].style.backgroundColor = 'red'
}
}
}
// This should be triggered on dom load / window load or in case when you want to check and mark the inputs with color
verifyInputs();
函数验证输入(){
var inputs=document.querySelectorAll('input');
对于(var i=0;i,如果您愿意,可以使用jQuery轻松地完成
HTML:
<input type="text" />
<input type="text" />
<input type="text" />
<input type="text" />
<input type="text" />
<input type="button" id="submitButton" value="Submit" />
//for javaScript
<!-- <input type="button" id="submitButton" value="Submit" onclick="validateField();" /> -->
function validateField(){
var textFields = document.getElementsByTagName("input");
for(var i=0; i < textFields.length; i++){
if(textFields[i].type == "text" && textFields[i].value == "")
{
textFields[i].style.backgroundColor = "red";
}
else {
textFields[i].style.backgroundColor = "white";
}
}
}
$("#submitButton").click(function(){
$("input").each(function(){
if($(this).attr("type") == "text" && $(this).val() == "")
{
$(this).css("background-color", "red");
}
else {
$(this).css("background-color", "white");
}
});
});
是的,这是可能的。你试过什么?我还是被卡住了,因为我对javascript非常陌生:(感谢您的及时回复,但我仍然无法使用提交按钮验证这些值。您是否将此函数绑定到提交按钮的单击事件?谢谢,最佳答案,+1还包括使用javaScript的解决方案。您可以检查!!