【问题标题】:How to simulate an autofocus如何模拟自动对焦
【发布时间】:2018-06-06 08:21:02
【问题描述】:

我为我们的网站创建了一个 webview (Android)。 我把输入放在自动对焦中:

<input name="barcode" 
        type="search"
        placeholder="Scan Barcode"
        class="form-control"
        autocomplete="off"
        autofocus
        required>

加载页面时选择了输入但Android的键盘没有打开=>完美

但是当我专注于这个输入时,android 键盘会打开,我不希望它打开,因为这个输入只用于扫描条形码

所以我想“模拟”关闭模态时的自动对焦(例如)而不是简单的对焦,怎么办?

$('.modal').on('hidden.bs.modal', function (e) {
  $("input[name='barcode']").focus();
});

编辑:更多细节:

我们使用这些扫描仪访问我们的内部网站:

https://www.jmprime.co.uk/product_info.php/android-barcode-scanner-with-gun-grip-rugged-handheld-ip67-device-with-1d-barcode-reader-p-238

在有问题的页面上只有一个输入,目标是在不打开安卓键盘的情况下输入输入,以便扫描条形码(然后将它们发送到数据库)

【问题讨论】:

  • 焦点后你试试.click?喜欢$("input[name='barcode']").focus().click()
  • @Rocstar 扫描仪通常会自动将其输入框中。您可以尝试捕获按键事件,然后使用 JavaScript 将其添加到框本身。这样它仍然可以是只读的。
  • 试试我最后的小提琴。它应该工作。它适用于我的 Galaxy S8,键盘不显示。你只需要看看条形码是否被正确扫描
  • 您在标签内添加一个跨度...它会自动被覆盖...如果 $barcode.val() 为空,您只需恢复它!
  • 我更新了小提琴来处理占位符jsfiddle.net/ubdnvsk0/26

标签: javascript jquery html


【解决方案1】:

截至This answer,只读似乎工作。 我发现很难直接在输入上工作,所以我仍然会使用假输入的技巧。 将timeout 设置为1ms,它就像魅力一样。 (下面更新小提琴)

<input id="barcode" name="barcode" type="hidden" placeholder="Scan Barcode" required>

<label for="barcode" class="your-cool-class-to-make-this-look-like-an-input" />

带有for属性,点击后会聚焦相关输入。

https://jsfiddle.net/ubdnvsk0/26/

【讨论】:

  • 期待你的榜样
  • 唉,它不起作用..当我点击带有 id 测试或标签的输入时,Android 键盘打开
【解决方案2】:

试试这个:

我将 Bootstrap 4 用于 modal 和 jQuery。

我忘记了为什么 .focus() 有时不执行,但我记得通过将它放入 setTimeout()0 的时间,它就可以了。

以防您对代码感到困惑:() =&gt; {} 类似于 function() {},这是一个anonymous function / callback

箭头函数是一种更简洁的函数编写方式,介绍 在ES6

箭头函数是匿名函数,这意味着你 无法命名。箭头函数不绑定到this,它们不会创建 他们自己的this,因此使用了封闭的this

阅读材料:'Arrow Functions - developer.mozilla.org'

$(document).ready(() => {
  $('#myModal').on('hidden.bs.modal', () => {

    setTimeout(() => {
      $("#barcode").focus();
    }, 0);
  })
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>

<div class="container">

  <input id="barcode" name="barcode" type="search" placeholder="Scan Barcode" class="form-control" autocomplete="off" autofocus required readonly><br>

  <!-- Button to Open the Modal -->
  <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
    Open modal
  </button>

  <!-- The Modal -->
  <div class="modal" id="myModal">
    <div class="modal-dialog">
      <div class="modal-content">

        <!-- Modal Header -->
        <div class="modal-header">
          <h4 class="modal-title">Modal Heading</h4>
          <button type="button" class="close" data-dismiss="modal">&times;</button>
        </div>

        <!-- Modal body -->
        <div class="modal-body">
          Modal body..
        </div>

        <!-- Modal footer -->
        <div class="modal-footer">
          <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
        </div>

      </div>
    </div>
  </div>

</div>

【讨论】:

  • 是的,这太不可思议了,但我只是想到了这一点并尝试了我的小提琴。它似乎有效。
  • 其实我没想到。我想抓住所有的焦点。我使用超时来添加和删除只读属性。但我抓住了“焦点”事件。因为如果用户点击输入,使用您的代码,键盘就会打开。
  • 只需将readonly 参数添加到您的输入中。还是重点。我相应地编辑了我的代码。
  • "() => {} 与 function() {}" 相同是非常不准确的。 jQuery 几乎总是更好地与function 一起使用(出于一致性原因,以及强大的this 绑定)function
猜你喜欢
  • 2019-08-24
  • 1970-01-01
  • 2014-06-26
  • 2019-09-19
  • 2016-10-01
  • 1970-01-01
  • 2019-07-13
  • 1970-01-01
  • 2023-03-04
相关资源
最近更新 更多