0

我有一个带有字符串的文本框,并且想检查用户是否在文本框字符串之间或文本框字符串的末尾放置了文本。是否有任何 Javascript 函数可以执行此任务?我试过了createTextRange()setSelectionRange()但没有任何效果。

4

1 回答 1

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);

演示 ​</p>

于 2012-11-02T08:23:14.240 回答