更改文本框输入的背景颜色在为空时不起作用 [英] Changing background color of text box input not working when empty
本文介绍了更改文本框输入的背景颜色在为空时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果输入为空,我很难使用此 javascript 代码更改文本输入的背景颜色.
I am having a tough time with this javascript code to change the background color of a text input if the input is empty.
代码如下:
function checkFilled() {
var inputVal = document.getElementById("subEmail").value;
if (inputVal == "") {
inputVal.style.backgroundColor = "yellow";
}
}
我希望文本框一开始是黄色的.
I would expect the textbox to come out yellow at the beginning.
推荐答案
DEMO -->
http://jsfiddle.net/2Xgfr/829/
HTML
<input type="text" id="subEmail" onchange="checkFilled();">
JavaScript
function checkFilled() {
var inputVal = document.getElementById("subEmail");
if (inputVal.value == "") {
inputVal.style.backgroundColor = "yellow";
}
else{
inputVal.style.backgroundColor = "";
}
}
checkFilled();
注意:您正在检查值并将颜色设置为不允许的值,这就是它给您错误的原因.像上面那样尝试.
Note: You were checking value and setting color to value which is not allowed, that's why it was giving you errors. try like the above.
这篇关于更改文本框输入的背景颜色在为空时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文