文本框中输入更改背景颜色的空当不工作 [英] Changing background color of text box input not working when empty

查看:163
本文介绍了文本框中输入更改背景颜色的空当不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个艰难的时间与这个JavaScript code更改文本输入的背景颜色,如果输入的是空的。

I am having a tough time with this javascript code to change the background color of a text input if the input is empty.

下面是code:

function checkFilled() {
    var inputVal = document.getElementById("subEmail").value;
    if (inputVal == "") {
        inputVal.style.backgroundColor = "yellow";
                }
        }

例如: http://jsfiddle.net/2Xgfr/

我希望文本框出来黄色开头。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆