javascript - 获取 JavaScript oninput 函数来更改背景颜色?

标签 javascript html css

我有一些代码适用于用户填写详细信息的密码表单,然后只有当他们输入的数字/年龄大于指定的数字/年龄时,其中的 2 个方框才有效或可输入。我现在想更进一步,让用户明白这些框只能在特定时间编辑,方法是将它们着色为不同的颜色。由于某种原因,尽管 JS 中的新代码不起作用。 代码如下:

<div class="container">
  <div class="jumbotron" id="firstform">
    <h1>Sign up page</h1> 
    <form id="myform">

        <label>Username </label> <input type="text" name="uname" id="uname" data-placement="bottom" title="" data-original-title="Username must be unique" class="mytooltip"><br>

        <div class="pwordCheck">
            <label>Password </label> <input type="password" id="pword" data-placement="bottom" title="" onkeyup="passwordValidation(); return false;" data-original-title="Password must be more than 6 characters long" class="mytooltip"><br>
            <label>Confirm Password </label>  <input type="password" id="confpword"  onkeyup="passwordValidation(); return false;" data-placement="bottom" title="" data-original-title="Passwords must match" class="mytooltip">
            <span id="themessage" class="themessage"></span><br> 
        </div>

        <label>Email </label> <input type="email" id="e-mail"><br>

        <label>Age </label> <input type="number" id="age" oninput="ifOfAge(); return false;"><br>

        <label>Can you drive? </label> <input type="text" id="drive" disabled><br>

        <label>What is your occupation? </label> <input type="text" id="occupation" disabled><br>

      <input type="submit" value="Submit" onclick="usernameAlreadyExists(); return false;">
    </form>  

  </div>



</div>  

CSS:

input#drive{
    background-color: #999;
}

input#occupation{
    background-color: #999;
}

js:

function ifOfAge() {

  var age = document.getElementById("age");
  var drive = document.getElementById("drive");
  var occupation = document.getElementById("occupation");
  var white = "#fff";

  if (age.value >= 21) {
    drive.disabled = false;
    occupation.disabled = false;
  } else if (age.value >= 16) {
    drive.disabled = false;
    occupation.style.backgroundColor = white;
  } else {
    drive.disabled = true;
    occupation.disabled = true;
    drive.style.backgroundColor = white;
    occupation.style.backgroundColor = white;
  }


}

最佳答案

颜色必须用引号引起来。如下图。无论你在哪里使用白色,都用引号括起来

 drive.style.backgroundColor = "white";

关于javascript - 获取 JavaScript oninput 函数来更改背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32819273/

相关文章:

html - 如何获取最新的 IE 兼容模式,最高可达 IE9?

css - 图像宽度在 chrome 中为零

jquery - img 标签周围的空格会导致不同的布局

javascript - 当它已经旋转时自定义旋转一个div

javascript - 路由器 Prop 和自定义 Prop 与 typescript react 路由器DOM功能组件

javascript - 这些可见性设置适用于除 Safari 之外的所有浏览器。为什么?

html - 使图像仅通过其特定部分响应?

javascript - 在Javascript中找到单词中重复字母的最大数量

javascript - for...in 循环和原型(prototype)链

javascript - 在 iOS Safari 中禁用选择上下文菜单