javascript - How to perform .select() on jQuery masked text input - Stack Overflow

I'm usingplugin.There is an input text with defined mask:<input type="text" id=&

I'm using / plugin.

There is an input text with defined mask:

<input type="text" id="txtMyInput" class="FocusSense"/>

and a script:

$(document).ready(function () {
    jQuery(function ($) {            
        $("#txtMyInput").mask("?9.99");            
    });
    $(".FocusSense").focus(function () {
        this.select();
    });
})

As you can see, I would like select all in txtMyInput on focus but but alas! On focus, mask appears and lose .select().

What should I do to preserve mask and .select()?

I'm using http://digitalbush./projects/masked-input-plugin/ plugin.

There is an input text with defined mask:

<input type="text" id="txtMyInput" class="FocusSense"/>

and a script:

$(document).ready(function () {
    jQuery(function ($) {            
        $("#txtMyInput").mask("?9.99");            
    });
    $(".FocusSense").focus(function () {
        this.select();
    });
})

As you can see, I would like select all in txtMyInput on focus but but alas! On focus, mask appears and lose .select().

What should I do to preserve mask and .select()?

Share Improve this question edited Feb 9, 2022 at 22:26 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Aug 28, 2012 at 15:11 dllhelldllhell 2,0144 gold badges36 silver badges52 bronze badges 1
  • did you tried $(this).select(); ? – insomiac Commented Aug 28, 2012 at 15:33
Add a ment  | 

5 Answers 5

Reset to default 5

I think what you are looking for, is this :

$(document).ready(function () {
    jQuery(function ($) {            
        $("#txtMyInput").mask("?9.99");            
    });
    $(".FocusSense").focus(function (e) {
      var that = this;
      setTimeout(function(){$(that).select();},10);
      return false;
    });
});

setTimeout will "queue" the select() execution. So it will select the content after masking is pleted.

Working Demo

you need to use $(this) to get the current object.

$(document).ready(function () {
    jQuery(function ($) {            
        $("#txtMyInput").mask("?9.99");            
    });
    $(".FocusSense").focus(function () {
        $(this).select(); // instead of this.select();
    });
});

sory change focus change click function;

jQuery(".FocusSense").click(function() {
    this.focus();
    this.select(); 
});​

this.select() change jQuery(this).select();

$(".FocusSense").focus(function () {
    jQuery(this).select();
});

This is duplicate of 'jQuery masked input plugin. select all content when textbox receives focus' where I posted explanation for this fix.

imeout method is unnecessary!

$(".yourMaskedInput").attr("readonly", true).select().removeAttr("readonly");

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742268941a4412331.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信