简体   繁体   English

无法在选中的复选框上使用 JS 将 css 应用于输入字段

[英]Unable to apply css to input field using JS on checkbox checked

I'm trying to apply the css('color','red') on input field with id "pred" when checkbox is checked.当复选框被选中时css('color','red')我试图在 ID 为“pred”的输入字段上应用css('color','red') Currently code is successfully disabling the input field when checked but for some reason I'm unable to figure out how to apply styling in same code.当前代码在检查时成功禁用了输入字段,但由于某种原因,我无法弄清楚如何在相同的代码中应用样式。

I know how to do it with separate function but I would like to know how to do it within this code and learn what I'm doing wrong.我知道如何用单独的函数来做,但我想知道如何在这段代码中做到这一点,并了解我做错了什么。

<div class="w3-row">
  <div class="w3-col s1">
    <label>Za predmet:</label>
    <input class="w3-input w3-border w3-light-grey" type="text" required placeholder="Naš broj" style="width: 90%;" name="pred" id="pred" />
  </div>
  <div class="w3-col s3">
    <div style="padding-top: 20px;">
      <input class="w3-check" type="checkbox" id="predsvi" name="predsvi" value="da" onclick="var input = document.getElementById('pred'); if(this.checked){ input.disabled = true; input.css('color','red');}else{input.disabled=false; input.focus();}">
      <label for="predsvi"> Sve predmete</label>
    </div>
  </div>
</div>

Short code:短代码:

onclick="var input = document.getElementById('pred'); if(this.checked) { 
  input.disabled = true; 
  input.css('color','red');}else{input.disabled=false; input.focus();
}

Error for this version is input.css is not a function.此版本的错误是 input.css 不是函数。

If I do it like this i get no error but nothing happens.如果我这样做,我不会出错,但没有任何反应。

$('pred').css('color','red')

Changing color property of a disabled element is meaningless.更改禁用元素的颜色属性是没有意义的。 I think you want to change the placeholder color.我认为您想更改占位符颜色。

Please Note: Using inline JavaScript is not a good practice.请注意:使用内联 JavaScript 不是一个好习惯。

then try the following way:然后尝试以下方式:

 function myFunc(el){ var input = document.getElementById('pred'); if(el.checked){ input.disabled = true; input.focus(); input.classList.add('el-color'); } else{ input.disabled=false; input.classList.remove('el-color'); } }
 .el-color::placeholder { color: red; }
 <div class="w3-row"> <div class="w3-col s1"> <label>Za predmet:</label> <input class="w3-input w3-border w3-light-grey" type="text" required placeholder="Naš broj" style="width: 90%;" name="pred" id="pred" /> </div> <div class="w3-col s3"> <div style="padding-top: 20px;"> <input class="w3-check" type="checkbox" id="predsvi" name="predsvi" value="da" onclick="myFunc(this)"> <label for="predsvi"> Sve predmete</label> </div> </div> </div>

Change it to:将其更改为:

 <div class="w3-row"> <div class="w3-col s1"> <label>Za predmet:</label> <input class="w3-input w3-border w3-light-grey" type="text" required placeholder="Naš broj" style="width: 90%;" name="pred" id="pred" /> </div> <div class="w3-col s3"> <div style="padding-top: 20px;"> <input class="w3-check" type="checkbox" id="predsvi" name="predsvi" value="da" onclick="var input = document.getElementById('pred'); if(this.checked){ input.disabled = true; input.focus(); input.style.color= 'red';}else{input.disabled=false;}"> <label for="predsvi"> Sve predmete</label> </div> </div> </div>

the problem was in the input.css() and the error is that using the .css is not a function in HTMLInputElement.onclick the red color will appear after you start typing.问题出在 input.css() 中,错误在于使用 .css 不是 HTMLInputElement.onclick 中的函数,开始输入后会出现红色。

Use onchange event to detect the changes of the checkbox.使用onchange事件来检测复选框的变化。

 $(document).ready(function () { $('#predsvi').change(function () { let inputField = $('#pred'); if (this.checked) { inputField.css('color', 'red'); } else { inputField.focus(); } inputField.attr('disabled', this.checked); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="w3-row"> <div class="w3-col s1"> <label>Za predmet:</label> <input class="w3-input w3-border w3-light-grey" type="text" required placeholder="Naš broj" style="width: 90%;" name="pred" id="pred" /> </div> <div class="w3-col s3"> <div style="padding-top: 20px;"> <input class="w3-check" type="checkbox" id="predsvi" name="predsvi" value="da"> <label for="predsvi"> Sve predmete</label> </div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM