使用 JavaScript 更改输入字段的背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26390759/
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-22 22:49:43  来源:igfitidea点击:

Change Background color of input field using JavaScript

javascriptjquery

提问by Muhammad Rizwan

I am making a form and making input field to read only using JavaScript. I want to change the default color for read only attribute to green or yellow.

我正在制作一个表单并使输入字段只能使用 JavaScript 读取。我想将只读属性的默认颜色更改为绿色或黄色。

HTML

HTML

<input type="radio" name="sex" value="male">Male<br/>
<input type="radio" name="sex" value="female">Female <br/>
Age:<input type="text" name="age" id="age" size="20">
Hobbies:<input type="text" name="hobbies" id="hobbies" size="20"><br/>
Phone:<input type="text" name="phone" id="phone" size="20"><br/>

When someone click on female the input having Id's "age" and "phone" becomes reading only using JavaScript.

当有人点击女性时,具有 Id 的“年龄”和“电话”的输入变为仅使用 JavaScript 读取。

JS

JS

$(function() {
$("input[name='sex']").change(function() {
    if($(this).val() == "female") {
        $("#age").attr("disabled", true);
        style="backgroundcolor=green"
        $("#phone").attr("disabled", true);
    } else if($(this).val() == "male") {
        $("#age").attr("disabled", false);
        $("#phone").attr("disabled", false);
    }
});
});

I want to change the color of input field when it is read only.

我想在只读时更改输入字段的颜色。

回答by Himanshu Tyagi

Just use : .css()

只需使用:.css()

Sample : http://jsfiddle.net/cL7ngmda/

示例:http: //jsfiddle.net/cL7ngmda/

$("input[name='sex']").change(function() {
    if($(this).val() == "female") {
        $("#age,#phone").attr("disabled", true).css("background-color","#0F0");
    } else if($(this).val() == "male") {
        $("#age,#phone").attr("disabled", false).css("background-color","#FFF");
    }
});



另一种简单的方法是使用 CSS class班级http://jsfiddle.net/e83s7s80/7/http://jsfiddle.net/e83s7s80/7/

css

css

.bg-green{
    background-color:green;
}

js

js

$("input[name='sex']").change(function() {
     $("#age,#phone").attr("disabled", $(this).val() == "female").toggleClass("bg-green");
});


And there is yet another simple way using CSS selectorto do so. : http://jsfiddle.net/dqgqjya5/3/

还有另一种使用 CSS选择器的简单方法。:http: //jsfiddle.net/dqgqjya5/3/

Just add this css:

只需添加这个css

input:disabled{
    background-color:green;
}

js

js

$("input[name='sex']").change(function() {
    $("#age,#phone").attr("disabled", $(this).val() == "female");
});

Refer : https://developer.mozilla.org/en-US/docs/Web/CSS/:disabled

参考:https: //developer.mozilla.org/en-US/docs/Web/CSS/: disabled

回答by Levi Beckman

$("#age").css('background-color', 'green');

OR

或者

$(this).css('background-color', 'green');

回答by user2019037

You can try this:

你可以试试这个:

$("input[name='sex']").change(function () {
    if ($(this).val() == "female") {
        $("#age").attr("disabled", true);
        $("#age, #hobbies, #phone").css("background-color", "green");
        $("#phone").attr("disabled", true);
    } else if ($(this).val() == "male") {
        $("#age").attr("disabled", false);
        $("#phone").attr("disabled", false);
        $("#age, #hobbies, #phone").css("background-color", "white");
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="sex" value="male">Male
<br/>
<input type="radio" name="sex" value="female">Female
<br/>Age:
<input type="text" name="age" id="age" size="20">Hobbies:
<input type="text" name="hobbies" id="hobbies" size="20">
<br/>Phone:
<input type="text" name="phone" id="phone" size="20">
<br/>

回答by jonyjm

Why dont you use just css?

你为什么不只使用 css?

Define a style for inputs disabled in your css file, and just disable them with js like you are doing right now...

为您的 css 文件中禁用的输入定义一个样式,然后像您现在所做的那样使用 js 禁用它们...

For example:

例如:

input[disabled] { background-color:#F00; }

回答by Sergio

As a beginner this is my solution for changing the background color of an input field. I am not sure if this is the best practice, but I would like to share it with the experts here.

作为初学者,这是我更改输入字段背景颜色的解决方案。我不确定这是否是最佳实践,但我想与这里的专家分享。

    $("#name").focusin(function() {
      $(this).addClass("green");
    });

    $("#name").blur(function() {
      $(this).removeClass("green");

      if (!$(this).val()) {
        $(this).addClass("warning");
      } else {
        $(this).removeClass("warning");
        $(this).addClass("green");
      }
    });
.warning {
  background-color: red;
  color: white;
}

.green {
  background-color: rgba(144, 238, 144, 1);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="formulier">
  <form>
    <div class="form-group row">
      <label class="col-md-2">Name: </label>
      <div class="col-md-6">
        <input type="text" class="col-md-6" id="name" />
      </div>
    </div>
  </form>
</div>