javascript - 在 Javascript 中对输入字段进行分组

标签 javascript html forms dom-events addeventlistener

我正在尝试将我的姓名/号码/电子邮件字段组合在一起,以便我可以拥有一个事件监听器。我想让它在用户离开输入字段时播放音频。我尝试向父 .infofield 类添加一个事件,但它不适用于 onblur/focus 事件。我想知道如何在 Javascript 中处理这个问题?

        <section class="infofield">
            <audio id="leave" src= "blur.mp3"></audio>

            <section>
                <p class ="name">Name*</p>
                First: <br><input type="text" name="fname" required><br/>
                Last: <br><input type="text" name="lname" required><br/>
            </section>

            <section>
                <p class ="name">Phone Number*</p>
                <input type="text" name="number" required>
            </section>

            <section>
                <p>Email*</p>
                <input type="email" name="email" required>  
            </section>

        </section>

最佳答案

我没有看到任何困难,问题是什么?

const myForm = document.getElementById('my-form')
  ;
myForm.in_fields.querySelectorAll('input').forEach(inElm=>
  {
  inElm.onfocus=focusOn;
  inElm.onblur=focusOff;
  })
function focusOn(e)
  {
  WiGo.textContent = `evt focus on ${e.target.name}`  
  }
function focusOff(e)
  {
  WiGo.textContent = `evt blur on ${e.target.name}`  
  }  
  
/* previous version --------------------------------------------------
myForm.in_fields.onmouseover=e=>
  {
  WiGo.textContent = 'mouse is over Personal informations fields'
  }
myForm.in_fields.onmouseout=e=>
  {
  WiGo.textContent = 'mouse is out of Personal informations fields'
  }
----------------------------------------------------------------------*/  
#my-form{
  font-size: 14px;
  font-family: Arial, Helvetica, sans-serif;
}
fieldset {
  width:22em;
  padding: 0 1em;
}
legend::before,
legend::after {
   content: '\00a0'
}
label,input {
  float: left;
  margin: .4em;
}
input {
  width:11em ;
  padding: .2em;
  border-width: 1px;
  padding: .3em 5px;
}
label {
  display: block;
  clear: both;
  width: 9em;
  text-align: right;
  line-height: 1.5em;
}
label::after {
   content: '* :'
}
<form id="my-form">

  <!--audio id="leave" src= "blur.mp3"></audio -->

  <fieldset name="in_fields">
    <legend>Personal informations</legend>

    <label> First Name</label>
    <input type="text" name="fname" required>

    <label>Last Name</label>
    <input type="text" name="lname" required>

    <label>Phone Number</label>
    <input type="text" name="number" required>

    <label>Email</label>
    <input type="email" name="email" required>  
  </fieldset>

  <p id="WiGo" >What's going on ?</p>

</form>

关于javascript - 在 Javascript 中对输入字段进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60251552/

相关文章:

javascript - jQuery - 来自选择输入的隐藏输入值

reactjs - 在Formik中提交后如何重置/清空表单

jquery - 使用 ASP.NET MVC 和 JQuery 表单插件/文件上传检测 IsAjaxRequest()

javascript - 如何为 ImageMapster 创建 PHP 生成的工具提示?

javascript - 如何使用 Facebook 等单一输入验证电子邮件或电话号码

javascript - Vue中使用事件修饰符.prevent实现无重定向提交表单

javascript - 字体大小根据字数变化

perl - HTML::Form Handler 角色和字段

javascript - ES6 循环和 forEach 中的上下文和变量作用域

javascript - 在事件冒泡阶段删除祖先