jQuery 当使用jquery有很多文本框时如何设置焦点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18437017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:37:36  来源:igfitidea点击:

How to set focus when there are many textbox using jquery?

jquery

提问by Sujeeth

I want to set focus to the textboxwhen i click the text box. I tried the below code but am not getting as expected .

textbox当我单击文本框时,我想将焦点设置为。我尝试了下面的代码,但没有达到预期。

$('#txtDate').focus();

Thanks in advance.

提前致谢。

采纳答案by Anil kumar

This will work

这将工作

$('input').click(function() {
  $(this).focus();
})

回答by Prabu Parthipan

Demo

演示

Use Document.readyTry this..

使用Document.ready试试这个..

 $(function(){
    $("#txtDate").focus();
  });

回答by Umm E Habiba Siddiqui

write it using jQuery function as

使用 jQuery 函数将其编写为

$(function() {
  $("#txtDate").focus();
});

or if it also doesn't work then try

或者如果它也不起作用然后尝试

$(function() {
  $(document.getElementById("txtDate")).focus();
});

回答by Somnath Kharat

This will work:

这将起作用:

$('input[type="text"]').click(function() {
  $(this).focus();
})

回答by Umesh

Your question is not completely clear. But the following answer might make sense to you. And write a CSS as below.

你的问题不完全清楚。但以下答案可能对您有意义。并编写如下 CSS。

JS------
$('input').focus(function() 
    $(this).addClass("focus");
});

CSS----
.focus {
    border: 2px solid #AA88FF;//This you can set any color you want
    background-color: #FFEEAA;//This you can set any color you want
}

回答by veeresh k

I had same problem, this resolved my problem without any delay

我遇到了同样的问题,这立即解决了我的问题

setTimeout(function() {
  $('#goal-input').focus();
});

回答by abc123

Write the code in document ready function

在文档就绪函数中编写代码

$(document).on('ready', (function () {
  $('#txtDate').focus();
 }));

It should work..

它应该工作..

or you can write as in document ready

或者你可以写成文档准备好了

document.getElementById("txtDate").focus();