jQuery清除焦点上的输入文本 [英] jQuery clear input text on focus

查看:158
本文介绍了jQuery清除焦点上的输入文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 jQuery 脚本:

  $(document) .ready(function(){
$(':input:enabled:visible:first')。focus(); $ b $('。letters')。keyup(function(){
$ var $ this = $(this);
if($ this.val()。length> 1)
$ this.val($ this.val()。substr(0,1) );
$(this).next('input')。focus();
});
});

它会将焦点设置在第一个 input ='text'字段在页面加载。当用户输入一个字符时,它将把焦点移到下一个输入字段。它也将限制每个字段允许的字符数(目前是1个字符)。

我想知道是否有可能清除焦点上的输入字段的当前值。当用户使用cursror单击来关注字段,而且当 $(this).next('input')。focus(); 设置关注下一个输入字段。

也可以验证字符只允许使用字母字符吗?

解决方案

要过滤输入,请使用

  $('input')。on('keydown' ,函数(e){
if(!/ [az] | [AZ] /。test(String.fromCharCode(e.which)))
return false;
});

要清除上的输入字段, / code>& ($'code> $('input')。点击focusin',function(){
this.value ='';
});

请注意,当点击进入非集中的输入控件


$ b

Demo http://jsfiddle.net/xbeR2/


I have this jQuery script:

$(document).ready(function() {
    $(':input:enabled:visible:first').focus();
    $('.letters').keyup( function() {
        var $this = $(this);
        if($this.val().length > 1)
            $this.val($this.val().substr(0, 1));
            $(this).next('input').focus();
        });
});

It will set focus on the first input='text' field on page load. When a user enters a character it will move focus to the next following input field. It will also limit the number of characters allowed in each field (currently 1 character).

I wonder if it's possible to clear the current value of the input field on focus. Both when a user clicks with the cursror to focus the field but also when the $(this).next('input').focus(); sets focus on the next input field.

Also is it possible to validate the characters to only allow alphabetical characters?

解决方案

To filter the input, use

​$('input').on('keydown', function(e) {
    if( !/[a-z]|[A-Z]/.test( String.fromCharCode( e.which ) ) )
        return false;
});​​​​​​​​

To clear the input field on click & focus, use

$('input').on('click focusin', function() {
    this.value = '';
});

Be aware of that this event will fire twice, when you click into a non-focused input control in its current form.

Demo: http://jsfiddle.net/xbeR2/

这篇关于jQuery清除焦点上的输入文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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