jQuery的重点放在第一个文本字段,并将光标值设置为即时数据输入 [英] Jquery focus on first text field and set cursor at value end for instant data entry

查看:108
本文介绍了jQuery的重点放在第一个文本字段,并将光标值设置为即时数据输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在加载表单并将默认焦点放在第一个文本字段中。文本字段填充了默认值PW-。 (客户的第一批字符如PW-446,PW-9887)

他们只想在表单加载时输入数字,而不是点击结束字段等等来键入数字。光标应该在PW的末尾闪烁,并准备好进行数据输入。

我已经尝试了许多不同的方法,在StackOverflow上发现没有运气:-(

任何想法?提前致谢!
$ b

JQUERY

  $(document).ready(function ){

$('input:text:first')。focus();

});
$ b HTML

 < input type =textname =part_idsize =20value =PW  - > 


解决方案

以下是 示例



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ ).get(0);
var elemLen = el.value.length;

el.selectionStart = elemLen;
el.selectionEnd = elemLen;
el。 focus();
});

https: //developer.mozilla.org/en-US/docs/XUL/Property/selectionStart



https://developer.mozilla.org/en-US/docs/XUL/Property/selectionEnd



对于较老版本的IE,您可能需要使用: http://msdn.microsoft.com/en-us/library/ie/ms536401(v = vs.85).aspx


I am loading a form and defaulting focus to the first text field. The text field is populated with a default value "PW-". (The first characters of the customers part number like PW-446, PW-9887)

They want to just start typing the numbers upon form load instead of clicking to the end of the field, etc to type numbers. The cursor should be blinking at the end of the PW- and ready to go for data input.

I have tried many different methods found on StackOverflow with no luck :-(
Any ideas? Thanks in advance!

JQUERY

$(document).ready(function(){

    $('input:text:first').focus();

});

HTML

< input type="text" name="part_id" size="20" value="PW-">

解决方案

Here's an EXAMPLE:

$(document).ready(function(){
    var el = $("input:text").get(0);
    var elemLen = el.value.length;

    el.selectionStart = elemLen;
    el.selectionEnd = elemLen;
    el.focus();
});​

https://developer.mozilla.org/en-US/docs/XUL/Property/selectionStart

https://developer.mozilla.org/en-US/docs/XUL/Property/selectionEnd

For older versions of IE you may need to use: http://msdn.microsoft.com/en-us/library/ie/ms536401(v=vs.85).aspx

这篇关于jQuery的重点放在第一个文本字段,并将光标值设置为即时数据输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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