【问题标题】:Firing the input of a search bar with another button使用另一个按钮触发搜索栏的输入
【发布时间】:2022-01-09 04:23:01
【问题描述】:

我有一个 jQuery 问题:我想创建以下内容:我希望当我单击一个按钮时,我的光标将位于搜索栏的输入上,准备好输入。我该怎么做?到目前为止,我正在尝试这样的事情,但它不起作用。提前谢谢你

jQuery('.button').on('click', function(){
          jquery('.input').click();
        } )

【问题讨论】:

  • 使用focus() 而不是click()

标签: jquery button input click


【解决方案1】:

你需要使用focus()函数:https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus

你宁愿使用“$”而不是使用jQuery('.button');

$('.button').on('click', function(){
  $('.input').focus();
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="input">
<button class="button">Enter some text</button>

【讨论】:

    猜你喜欢
    • 2020-10-11
    • 2019-11-24
    • 1970-01-01
    • 2020-08-01
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    相关资源
    最近更新 更多