HTML forms: focus on checkbox after completing text field

旧时模样 提交于 2019-12-01 23:20:20

问题


Look at this HTML form and notice how it has a checkbox between text fields:

I tried to fill it up with Chrome on mobile and noticed an unexpected problem: if the user types into a text field, and then moves to the next field by pressing the blue "done" button in an Android keyboard, focus will jump over the checkbox into the next text field. This would be fine, except for the fact that the user never even sees the checkbox and thus may leave it unintentionally unchecked:

Is it possible to force focus on the checkbox field or something like that? If there is no technical solution, I'll be happy to accept a UX solution.

<form action="/action_page.php" method="get">
  <input name="text1" value="Some text"><br>
  <input name="text2" value="More text"><br>
  <input name="text3" value="Bla bla"><br>
  <input type="checkbox" name="vehicle" value="Bike"> I have a bike<br>
  <input name="text1" value="Some text"><br>
  <input name="text2" value="More text"><br>
  <input name="text3" value="Bla bla"><br>
  <input type="submit" value="Submit">
</form>

回答1:


change Event & .focus()

Try registering a change event on [name=text3] and trigger a .focus() on [name=vehicle]. When user has typed into [name=text3] then clicks elsewhere, a .focus() is triggered on [name=vehicle]

Demo

var blaBla = document.querySelector('[name=text3]');

var bike = document.querySelector('[name=vehicle]');

blaBla.addEventListener('change', goToChk);

function goToChk(e) {
  bike.focus();
}
[name=vehicle]:focus {
  outline: 3px solid red;
}
<form action="/action_page.php" method="get">
  <input name="text1" value="Some text"><br>
  <input name="text2" value="More text"><br>
  <input name="text3" value="Bla bla"><br>
  <input type="checkbox" name="vehicle" value="Bike"> I have a bike<br>
  <input name="text1" value="Some text"><br>
  <input name="text2" value="More text"><br>
  <input name="text3" value="Bla bla"><br>
  <input type="submit" value="Submit">
</form>


来源:https://stackoverflow.com/questions/54175698/html-forms-focus-on-checkbox-after-completing-text-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!