如何使用JavaScript从文本框控件中获取选定的文本 [英] How to get selected text from textbox control with javascript
问题描述
我有一个文本框和一个链接按钮。
当我编写一些文本时,然后选择其中一些文本,然后单击链接按钮,文本框中的选定文本必须显示一个消息框。
我该怎么做?
当我点击下面的文本框提交按钮,消息框必须显示Lorem ipsum。因为在该区域选择了Lorem ipsum。
如果我从页面中选择任何文本并单击提交按钮它正在工作,但是如果我写一个文本到文本框并创建它,它不是。因为当我点击另一个空间时,文本框的选择被取消。
现在的问题是,当我从文本框中选择一个文本并单击任何其他控件或空间时,所选文本仍然必须被选中。
这是怎么完成的?
我有这样的代码:$ p $
function ShowSelection()
{
var textComponent = document.getElementById('编辑');
var selectedText;
if(textComponent.selectionStart!== undefined)
{//符合标准的版本
var startPos = textComponent.selectionStart;
var endPos = textComponent.selectionEnd;
selectedText = textComponent.value.substring(startPos,endPos);
}
else if(document.selection!== undefined)
{// IE版本
textComponent.focus();
var sel = document.selection.createRange();
selectedText = sel.text;
}
alert(您选择了:+ selectedText);
$ / code> 问题是,虽然我给IE的代码是在很多站点上给出的,我无法在我目前的系统上使用IE6的副本。也许它适用于你,这就是为什么我给它。
您寻找的技巧可能是.focus()调用,将焦点返回给textarea,以便重新激活选择。 / p>
[UPDATE]我用onKeyDown事件得到了正确的结果(选择内容):
document.onkeydown = function(e){ShowSelection(); }
所以代码是正确的。再次,问题是要点击一个按钮上的选择...我继续搜索。
[更新]我没有成功与一个按钮绘制一个 li
标记,因为当我们点击它时,IE会取消选择上一个选择。上面的代码使用一个简单的输入
按钮,尽管...
I have a textbox and a link button.
When I write some text, then select some of them and then click the link button, selected text from textbox must be show with a messagebox.
How can I do it?
When I click the submit button for textbox below, message box must show Lorem ipsum. Because "Lorem ipsum" is selected in the area.
If I select any text from the page and click the submit button it is working, but if I write a text to textbox and make it, it's not. Because when i click to another space, selection of textbox is canceled.
Now problem is that, when i select a text from textbox and click any other control or space, text which is selected is must be still selected.
How it is be done?
解决方案 OK, here is the code I have:
function ShowSelection()
{
var textComponent = document.getElementById('Editor');
var selectedText;
if (textComponent.selectionStart !== undefined)
{// Standards Compliant Version
var startPos = textComponent.selectionStart;
var endPos = textComponent.selectionEnd;
selectedText = textComponent.value.substring(startPos, endPos);
}
else if (document.selection !== undefined)
{// IE Version
textComponent.focus();
var sel = document.selection.createRange();
selectedText = sel.text;
}
alert("You selected: " + selectedText);
}
Problem, although the code I give for IE is given on lot of sites, I cannot make it work on my copy of IE6 on my current system. Perhaps it will work for you, that's why I give it.
The trick you look for is probably the .focus() call, to give back to textarea the focus so the selection is re-activated.
[UPDATE] I got the right result (the selection content) with onKeyDown event:
document.onkeydown = function (e) { ShowSelection(); }
So the code is correct. Again, the issue is to get the selection on click on a button... I continue to search.
[UPDATE] I got no success with a button drawn with a li
tag, because when we click on it, IE deselects the previous selection. The above code works with a simple input
button, though...
这篇关于如何使用JavaScript从文本框控件中获取选定的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!