最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Unselect textbox on blur with jQuery in IE8 - Stack Overflow

programmeradmin1浏览0评论

I'm trying to have the default behavior of a form select (or highlight) the text when you tab to the next input. I've had to add the .select() function on my password form fields for this to work in IE8. Is there an equivalent to the jquery .select() for deselecting text?

       $("#MyTextBox").focus(function() {
            $(this).select();
        });

        $("#MyTextBox").blur(function() {
            // Deselect here
        });

I'm trying to have the default behavior of a form select (or highlight) the text when you tab to the next input. I've had to add the .select() function on my password form fields for this to work in IE8. Is there an equivalent to the jquery .select() for deselecting text?

       $("#MyTextBox").focus(function() {
            $(this).select();
        });

        $("#MyTextBox").blur(function() {
            // Deselect here
        });
Share Improve this question asked Feb 22, 2010 at 7:26 VictorVictor 5,1076 gold badges32 silver badges47 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Not that I am aware, however this ought to work for you:

$(this).val($(this).val());

Here you set the value of the field to itself. the cursor should automatically be put on the end.

The posted answer by James Wiseman worked perfectly in IE8, but for me at least, didn't work in Firefox 3.6.x.

I appreciate that the original question is specific to IE8, however, to help out those searching for a single solution that works in FF and IE, I used the following code:

var temptext = $(textbox).val();
$(textbox).val('');
$(textbox).val(temptext);

(where textbox is the textbox object to work with).

It uses the same theory as James Wiseman's solution, however, it adds the specific step of setting the textbox's text to a blank string prior to setting the textbox text back to the original string. Hope this helps!

发布评论

评论列表(0)

  1. 暂无评论