2012-11-02 78 views
0

我有一個帶有字符串的文本框,並且想要檢查用戶是否在文本框字符串之間或文本框字符串末尾丟棄了文本。是否有任何JavaScript功能來完成這項任務?我試過createTextRange()和setSelectionRange(),但沒有任何工作。如何檢查文本框內丟棄的文本位置?

回答

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