jQuery $ .focus()不能使用id和输入类型 [英] jQuery $.focus() not working with id and with input type

查看:499
本文介绍了jQuery $ .focus()不能使用id和输入类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题:

我有一个模式,用ajax搜索结果后打开:打开并正确显示所有请求的结果。



最后,我需要清空ID为results的另一个div,清空一个ID为search的输入,然后重点关注它:


  • 空的结果正在工作

  • 更改search的值为 >关注搜索不起作用



这是我的HTML:

 < div class =col-sm-3> 
< form action =method =postid =searchit>
< div class =input-group mar-btm>
< span class =input-group-addon>< i class =fa fa-search fa-lg>< / i> Ricerca:其中/跨度>
< input type =textid =searchtabindex =1class =form-controlname =searchplaceholder =Codice,EAN o Testo/>
< / div>
< / form>
< / div>
< div class =col-sm-4id =results>< / div>

更新:
jQuery:

$ b ()。
$ b

  $(。modal)。('hidden.bs.modal',function(){
$(#results ).empty();
$(#search)。val('');
$(#search)。focus();
});

我也试过:

($ .c $ c $)$('。modal)。('hidden.bs.modal',function(){
$(#results)。empty();
$ (#search)。val('')。focus();
});

和(是此页面唯一输入的文字):
$ ()。$ b $ $ $ $ $ $$ mod $。('hidden.bs.modal',function(){
$(#results)。 ();
$(#search)。val('');
$('input [type =text]')。focus();
});在任何情况下,empty()和val()都能正常工作,但没有任何工作与焦点(())。 )!

我不知道我错过了什么/做错了!



有些帮助?提前致谢!

解决方案尝试使用 .focus()在一个事件处理程序中,通常来自被处理的事件对焦点有影响。为了解决这个问题,我通常只是在超时时间内将 .focus()调用换行: ()。$($。$'$'$'$'$。$'$'$。$'$' #searc)。val('');
setTimeout(function(){
$(#searc)。focus();
},10);
});

超时处理程序将在任何触发事件处理程序完成后立即运行。


I have a little problem:

I have a modal that opens after some result search with ajax: it opens and show all requested results correctly.

On close, I need to empty another div with ID "results", empty an input with ID "search" and focus on it:

  • empty "results" is working
  • changing value of "search" to "" is also working
  • focus on "search" is not working

Here's my HTML:

<div class="col-sm-3">
   <form action="" method="post" id="searchit">
      <div class="input-group mar-btm">
         <span class="input-group-addon"><i class="fa fa-search fa-lg"></i> Ricerca:</span>
         <input type="text" id="search" tabindex="1" class="form-control" name="search" placeholder="Codice, EAN o Testo" />
       </div>
   </form>
 </div>
 <div class="col-sm-4" id="results"></div>

UPDATE: jQuery:

$(".modal").on('hidden.bs.modal', function () {
     $("#results").empty();
     $("#search").val('');
     $("#search").focus();
});

I also tried:

$(".modal").on('hidden.bs.modal', function () {
     $("#results").empty();
     $("#search").val('').focus();
});

and (is the only text input on this page):

$(".modal").on('hidden.bs.modal', function () {
     $("#results").empty();
     $("#search").val('');
     $('input[type="text"]').focus();
});

In every case, empty() and val() are working properly but nothing is working with focus()! No error thrown in console...

I can't figure out what I'm missing/doing wrong!

Some help? Thanks in advance!

解决方案

Problems with trying to use .focus() in an event handler usually stem from the event being handled also having an affect on focus. To get around that, I usually just wrap the .focus() call in a timeout:

$(".modal").on('hidden.bs.modal', function () {
     $("#results").empty();
     $("#searc").val('');
     setTimeout(function() {
       $("#searc").focus();
     }, 10);
});

The timeout handler will run basically immediately after whatever triggered the event handler is all finished.

这篇关于jQuery $ .focus()不能使用id和输入类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆