admin管理员组

文章数量:1332620

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");

本文标签: javascriptHow to perform select() on jQuery masked text inputStack Overflow