13

我知道使用 javascript,您可以使用以下代码(在 jQuery 中)选择文本框的内容:

$("#txt1").select();

有没有办法做相反的事情?取消选择文本框的内容?我将一系列文本框的焦点事件设置为选择其中的内容。现在有时我想关注一个特定的文本框而不选择它。我打算做的是为这个特定的文本框调用焦点事件,然后调用它来取消选择它。

$("input[type=text]").focus(function() {
    $(this).select();
});

//code....

$("#txt1").focus();

//some code here to deselect the contents of this textbox

有任何想法吗?

谢谢!

4

8 回答 8

19

那这个呢:

$("input").focus(function(){
  this.selectionStart = this.selectionEnd = -1;
});
于 2010-06-08T14:34:00.957 回答
8

如果您只是将文本框的值分配给自身,它应该取消选择文本。

于 2009-04-27T18:14:05.197 回答
3

您需要设置selectionStartandselectionEnd属性。但是由于某种原因,将这些设置在焦点事件上不起作用(我不知道为什么)。要使其工作,请在一小段时间后设置属性。

  $(document).ready(function(){
    $('#txt1').focus(function(){
      setTimeout(function(){
        // set selection start, end to 0
        $('#txt1').attr('selectionStart',0);
        $('#txt1').attr('selectionEnd',0);
      },50);  // call the function after 50ms
    });
  });
于 2011-02-08T13:25:03.920 回答
1

要“聚焦特定的文本框而不选择它”:我会使用修补的 jquery 插件 jquery-fieldselection 的一部分

使用它你可以打电话

$('#my_text_input').setSelection({"start":0, "end":0}); // leaves the cursor at the left

或者使用这个将光标放在文本末尾的简化版本(默认情况下)

(function() {
var fieldSelection = {
    setSelection: function() {
        var e = (this.jquery) ? this[0] : this, len = this.val().length || ;
        var args = arguments[0] || {"start":len, "end":len};
        /* mozilla / dom 3.0 */
        if ('selectionStart' in e) {
            if (args.start != undefined) {
                e.selectionStart = args.start;
            }
            if (args.end != undefined) {
                e.selectionEnd = args.end;
            }
            e.focus();
        }
        /* exploder */
        else if (document.selection) {
            e.focus();
            var range = document.selection.createRange();
            if (args.start != undefined) {
                range.moveStart('character', args.start);
                range.collapse();
            }
            if (args.end != undefined) {
                range.moveEnd('character', args.end);
            }
            range.select();
        }
        return this;
    }
};
jQuery.each(fieldSelection, function(i) { jQuery.fn[i] = this; });
})();

这样使用:

$('#my_text_input').setSelection(); // leaves the cursor at the right
于 2012-04-16T15:14:53.903 回答
0

与其选择然后取消选择,不如在 dom 元素上临时存储一个布尔值?

 $("input[type=text]").focus(function() {
     if($(this).skipFocus) return;
     $(this).select();
 });

 //code....

 $("#txt1").skipFocus = true;
 $("#txt1").focus();
于 2009-04-27T18:19:08.617 回答
0

我想提出一个简单的解决方案

$("input[type=text]").focus(function() {
    $(this).select();
});

$("input[type=text]").blur(function() {
    $('input[type="hidden"][value=""]').select();
});

//code....

$("#txt1").focus();
于 2012-06-15T23:01:36.307 回答
0

这是一个没有jquery的简单解决方案

<input type="text" onblur="this.selectionStart = this.selectionEnd = -1;">
于 2015-03-04T23:43:34.960 回答
-1

如果要使用 jQuery 取消选择文本框,请执行以下操作:

   $(your_input_selector).attr('disabled', 'disabled');
   $(your_input_selector).removeAttr('disabled');
于 2010-06-08T14:31:00.853 回答