简体   繁体   English

JavaScript 在用户填写或不填写文本字段时更改文本字段

[英]JavaScript Change the text fields when the user fills or not when the text field loses focus

i need help with an assignment: When a user fills in a text field and the text field loses focus, make the text field green with a check mark.我需要一项作业的帮助:当用户填写文本字段并且文本字段失去焦点时,用复选标记将文本字段设为绿色。 You can use a check mark from FontAwesome.您可以使用 FontAwesome 中的复选标记。 When a user doesn't fill in a text field, but removes focus, make the text field red with a cross I need the javascript/dom code to complete this assignment.当用户未填写文本字段但移除焦点时,将文本字段设为红色并带有叉号 我需要 javascript/dom 代码来完成此任务。 I have tried this code but doesnt work:我试过这段代码但不起作用:

    let fields = document.getElementsByTagName("input");
const checkFields = function () {
    for (let i = 0; i < fields.length; i++) {
        if (fields[i].value == "") {
          fields[i].style.backgroundColor = "red";
        } else {
          fields[i].style.backgroundColor = "green";
        }
      }
}
fields.addEventListener('change', checkFields)



          <div class="controls">
            <input
              autofocus=""
              class="input-xlarge"
              id="company"
              name="company"
              placeholder="Company name"
              type="text"
              value=""
            />
          </div>
        </section>

        <section class="control-group">
          <label class="control-label" for="fname">Name</label>

          <div class="controls two-col">
            <input
              class="input-medium"
              id="fname"
              name="fname"
              placeholder="First name"
              type="text"
              value=""
            />
          </div>

          <div class="controls two-col">
            <input
              class="input-medium"
              id="lname"
              name="lname"
              placeholder="Last name"
              type="text"
              value=""
            />
          </div>
        </section>

        <section class="control-group">
          <label class="control-label" for="email">Email</label>

          <div class="controls">
            <input
              class="input-xlarge"
              id="email"
              name="email"
              placeholder="Email"
              type="email"
              value=""
            />
          </div>
        </section>

Here is your solution:这是您的解决方案:

 // Select Each Input document.querySelectorAll('input').forEach((inp) => { // Remove whitespace from value and check it // Add eventlistener on each input inp.addEventListener('change', () => { let value = inp.value.split(' ').join('') // Do something with value; if (value == "") { inp.style.backgroundColor = "red"; } else { inp.style.backgroundColor = "green"; } }) })
 <section> <div class="controls"> <input autofocus="" class="input-xlarge" id="company" name="company" placeholder="Company name" type="text" value="" /> </div> </section> <section class="control-group"> <label class="control-label" for="fname">Name</label> <div class="controls two-col"> <input class="input-medium" id="fname" name="fname" placeholder="First name" type="text" value="" /> </div> <div class="controls two-col"> <input class="input-medium" id="lname" name="lname" placeholder="Last name" type="text" value="" /> </div> </section> <section class="control-group"> <label class="control-label" for="email">Email</label> <div class="controls"> <input class="input-xlarge" id="email" name="email" placeholder="Email" type="email" value="" /> </div> </section>

You can use "focusout" event which I believe will be a better choice if I understood your problem right.您可以使用“focusout”事件,如果我正确理解您的问题,我相信这将是更好的选择。

const ips = document.querySelectorAll("input")

ips.forEach(ip=>ip.addEventListener("focusout", ()=>{
    const text = ip.value
    if (text === "") ip.style.border= "1px solid red"
    else ip.style.border = "1px solid green"
 })
)

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

相关问题 Javascript:输入字段失去焦点时触发更改事件 - Javascript: change event getting fired when input field loses focus 单击时如何将一个文本字段的旧值与失去焦点时的同一文本字段的值进行比较 - How to compare old value of one text field when it is clicked, with the value of the same text field when it loses a focus 填满JavaScript时切换到下一个文本区域 - Change to next text area when one fills JavaScript 悬停DIV时文本框失去焦点 - Text box loses focus when DIV is hovered MUI 自定义文本字段失去焦点 state 变化 - MUI Custom Text Field loses focus on state change 用户单击禁用的href链接时如何关注文本字段? - How to focus on text field when user clicked on a disabled href link? JavaScript:“浏览器”选项卡失去用户焦点时发生错误 - JavaScript: Errors occur when Browser tab loses user focus 当元素失去焦点时,如何在一个contenteditable元素中保留文本? - How to keep text selected in a contenteditable element when the element loses focus? 当用户使用Jquery清除文本字段并将焦点移出文本字段时,如何触发事件? - How to trigger an event when the user clears the text field and focus out of the text field using Jquery? jquery,当字段失去焦点时提交表单 - jquery, submit form when field loses focus
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM