【问题标题】:Grouping input fields in Javascript在 Javascript 中对输入字段进行分组
【发布时间】:2020-02-16 18:06:47
【问题描述】:

我正在尝试将我的姓名/号码/电子邮件字段组合在一起,以便我可以拥有一个事件侦听器。我想让它在用户离开输入字段时播放音频。我尝试向父 .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>

【问题讨论】:

  • 使用&lt;fieldset&gt; & &lt;legend&gt; developer.mozilla.org/en-US/docs/Web/HTML/Element/fieldset
  • @MisterJojo 我使用了 fieldset,它适用于 onclick 事件,但不适用于模糊/焦点事件。我不确定我做错了什么。
  • 我只是说,如果你想对字段输入进行分组,你必须使用 fieldset html 标签

标签: javascript html forms dom-events addeventlistener


【解决方案1】:

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

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>

【讨论】:

  • 问题在于它适用于 mouseover/mouseout 事件,但不适用于 blur/focus 事件
  • 我不确定这是否正确,但我认为这是因为 mouseover/out 将事件发送到元素本身以及其中的任何内容,而 blur 事件仅检测到父元素失去焦点。我想弄清楚如何定位我的所有输入字段,以便为所有输入字段添加一个事件侦听器
猜你喜欢
  • 2013-10-06
  • 1970-01-01
  • 1970-01-01
  • 2021-09-18
  • 2011-01-15
  • 1970-01-01
  • 2017-02-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多