如何检查文本框中的删除文本位置?

时间:2012-11-02 08:01:37

标签: javascript-events xul javascript unobtrusive-javascript

我有一个带字符串的文本框,想要检查用户是否在文本框字符串之间或文本框字符串末尾删除了文本。是否有任何javascript函数来执行此任务?我尝试了createTextRange()和setSelectionRange()但没有任何作用。

1 个答案:

答案 0 :(得分:0)

<强> HTML

<input type="text" id="myTextInput" value="test" />​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

<强> JS

var myInput = document.getElementById('myTextInput');
var defaultValue = myInput.value;

myInput.addEventListener("input", function () { 

    var index = myInput.value.indexOf(defaultValue, 0);
    if(index == 0)
        alert('The text has been added at the end of the original string');
    else if(index == -1)
        alert('The text has been added inside the original string');
    else
        alert('The text has been added in the front of the original string');

}, false);

DEMO