简体   繁体   English

在聚焦时,在输入字段中选择一些文本,但不是全部

[英]Select some text in an input field, but not all, when it is focused

If I have a text field like <input type="text" name="summary" value="Summary: This is a test."> , is it possible to make it so that when the user clicks on the field, or uses Tab to reach it, that rather than select all of the text, only part of it is selected? 如果我有一个文本字段,如<input type="text" name="summary" value="Summary: This is a test."> ,是否可以使用当用户点击该字段或使用时选项卡到达它,而不是选择所有文本,只选择其中的一部分?

In this case, I'd like the This is a test part to be selected, so the user can immediately start typing to replace it with what they want. 在这种情况下,我希望This is a test要选择This is a test部分,因此用户可以立即开始输入以将其替换为他们想要的内容。

Is this possible with JavaScript, and if necessary, some type of hack to make it work? 这是否可以使用JavaScript,如果有必要,可以使用某种类型的hack来使其工作?

Unfortunately, I am quite limited by what other options I have in this case; 不幸的是,我对这种情况下的其他选择非常有限; typically, alternatives would be to place the Summary: outside the text box, for instance, so it can't be edited. 通常,替代方案是将Summary:放在文本框之外,因此无法编辑。

yes man it is possible... here I have an example that selects a part of your fields text... 是的,这是可能的...这里我有一个例子,选择你的领域文本的一部分......

$.fn.selectRange = function(start, end) {
    return this.each(function() {
        if (this.setSelectionRange) {
            this.focus();
            this.setSelectionRange(start, end);
        } else if (this.createTextRange) {
            var range = this.createTextRange();
            range.collapse(true);
            range.moveEnd('character', end);
            range.moveStart('character', start);
            range.select();
        }
    });
};

$('input').selectRange(6,15);

http://jsfiddle.net/WpqsN/1972/ http://jsfiddle.net/WpqsN/1972/

so you can use it like this: 所以你可以像这样使用它:

$('input').click(function(){
    $('input').selectRange(6,15);
});

or 要么

$('input').one('click',function(){
    $('input').selectRange(6,15);
});

to run only on FIRST click 仅在第一次点击时运行

Alright - this attempts creates an "uneditable" field name inside the <input> 's value. 好吧 - 这次尝试在<input>的值中创建了一个“不可编辑的”字段名称。 The reason it attempts but isn't completely successful is because it actually is editable, but if the user changes the field name, it tries to recover the value and restore the field name. 它尝试但未完全成功的原因是因为它实际上是可编辑的,但如果用户更改了字段名称,它会尝试恢复该值并恢复字段名称。

Here's the Javascript: 这是Javascript:

var Fields = {
  Seperator:":"
}

Fields.copyFieldName = function (element) {
  if (element.dataset.fieldname) {
    var seperatorIndex = Fields.findSeperator(element); 
    if (element.value.substring(0, seperatorIndex) !== element.dataset.fieldname) {
      var oldValue = element.value.substring(seperatorIndex + 1, element.value.length)
      element.value = element.dataset.fieldname + Fields.Seperator + oldValue;
    }
  }
};

Fields.findSeperator = function (element) {
  return element.value.indexOf(Fields.Seperator); 
}; 

Fields.selectInputValue = function (element) {
  if (element.type === "text") {
    if (element.setSelectionRange) {
      element.setSelectionRange(
        Fields.findSeperator(element) + 1, 
        element.value.length
      );
    } else if (element.createTextRange) {
      var range = element.createTextRange();
      range.moveStart("character", Fields.findSeperator(element) + 1);
      range.moveEnd("character", element.value.length);
      range.select();
    }
  }
};


$(document).ready(function () {
  $("input[data-fieldname]").each(function (index, element) {
    Fields.copyFieldName(element); 

    $(element).click(function () {
      Fields.copyFieldName(element);
      Fields.selectInputValue(this);
    });

    $(element).focusin(function (e) {
      e.preventDefault();
      Fields.copyFieldName(element);
      Fields.selectInputValue(this);
    }); 
  });
});

And HTML: 和HTML:

<input type="text" name="extra1" value="an extra field" />
<input type="text" name="summary" value="this is a test" data-fieldname="Summary" />
<input type="text" name="extra2" value="an extra field" />

Please note, I made a small modification to you field example. 请注意,我对你的字段示例做了一个小修改。 I use a data-* attribute to store the name of the field. 我使用data-*属性来存储字段的名称。

Here's a fiddle: http://jsfiddle.net/tFRD4/4/ 这是一个小提琴: http//jsfiddle.net/tFRD4/4/

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM