【问题标题】:how to set focus on input when after dropdown value is selected in web page在网页中选择下拉值后如何设置输入焦点
【发布时间】:2022-01-24 15:04:16
【问题描述】:

我有下拉我希望当用户从下拉列表中选择任何值然后在输入字段中设置光标的焦点。但这也应该适用于 ipad。

<!DOCTYPE HTML>
<html lang = "en">
<head>
<title>formDemo.html</title>
<meta charset = "UTF-8" />
</head>
<body>
<h1>Form Demo</h1>
<form>
<legend>Selecting elements</legend>
<p>
 <label>Select list</label>
         <select id = "myList">
           <option value = "1">one</option>
           <option value = "2">two</option>
           <option value = "3">three</option>
           <option value = "4">four</option>
         </select>
      </p>

  <input type="text" name="txt1" > 


  <form>

【问题讨论】:

    标签: javascript html ipad drop-down-menu web


    【解决方案1】:
    <select id = "myList" onchange="document.getElementsByName('txt1')[0].focus()">
    

    【讨论】:

    • 抱歉,safari 5-中没有“聚焦”功能,这是ipad使用的。
    【解决方案2】:

    试试这个

    window.addEventListener("DOMContentLoaded", function() {
      document.getElemmentById("myList").addEventListener("change", function() {
        document.querySelector("[name=txt1]").focus();
      } 
    }
    

    在 iOS 或至少在移动 Safari 中我仍然无法获得焦点,因为它会调出键盘,而这不一定是用户想要的,因此 Apple 禁止使用脚本进行焦点。 SetFocus does not work on iPad

    【讨论】:

      猜你喜欢
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-30
      • 2021-03-17
      • 1970-01-01
      相关资源
      最近更新 更多