Javascript 如何通过javascript更改文本框背景颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3414633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:31:51  来源:igfitidea点击:

how to change textbox background color through javascript?

javascript

提问by nectar

my javascript-

我的 javascript-

function validate_loginform(loginform) 
{
var uid = loginform.uid.value;
var pass = loginform.pass.value;
if(uid == "") 
  {

    color('uid');       
    return false;
  }
if(pass == 0) 
  {
    color('pass');
    return false;
  }

return true;

}

}

function color(traget)
{
var targetbox = document.getElementById(target);
targetbox.style.backgroundColor="red";
}

but background color is not getting changed even it is not returning faslevalue. if I remove the color('uid');nad put alert("user name required");then this script is working fine.Whats wrong?
it backgroundColor in actual program I just missed it here only

但是即使没有返回fasle值,背景颜色也不会改变。如果我删除color('uid');nad putalert("user name required");那么这个脚本工作正常。有什么问题?
它在实际程序中的 backgroundColor 我只是在这里错过了它

回答by Maciej Kucharz

With jQuery you could try this:

使用 jQuery 你可以试试这个:

 $("#textbox").css("background-color", "red");

回答by nectar

dont call color function, change color inside if condition like-

不要调用颜色函数,如果条件像 - 在里面改变颜色 -

if(uid == "") 
  {     
    //alert("You must enter User ID.","error");
    loginform.uid.style.borderColor='red';
    loginform.uid.focus();
    return false;
  }

回答by PPShein

Beware your spelling. It should be "target", not "traget".

注意你的拼写。它应该是“目标”,而不是“目标”。

function color(traget)

功能颜色(目标)

回答by álvaro González

Typo?

错别字?

backgroungColor
         ^

Update

更新

Typo?

错别字?

function color(traget)
               ^^^^^^
{
var targetbox = document.getElementById(target);

Seriously, actual code does matter.

说真的,实际代码确实很重要。

回答by user395702

just remove the single quote (') from color('uid')

只需从 color('uid') 中删除单引号 (')

and write it as color(uid);

回答by Danny Nimmo

You've spelt target wrong in your function header and background wrong in the last line of the function.

您在函数标题中拼写错误,在函数的最后一行拼写错误背景。