最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove last character on input field jQuery - Stack Overflow

programmeradmin2浏览0评论

I know this kind of question has been asked several times but none of the answers worked for me. I want to make my own spell-checker which pares words letter by letter and checks the correctness. As user types letters one by one, the content is checked. If it is correct, goes on, if it is wrong: I want the last letter to be deleted.

Here is my code:

var word = [];
word = "beautiful".split('');
var i =0;

$(document).ready(
function() 
{ $('#txtword').keyup(
 function() {
    var value = document.getElementById('txtword').value;
    if (value[i] == word[i]){ 
            alert("Right letter!");
            i++; }
            else
            { alert("Wrong letter");
            value.slice(0,-1);
            /*value.substr(0,value.length-1);*/
            }
        });
    })

I tried both options value.slice(0,-1); and value.substr(0,value.length-1); but they are not working. Can someone help me find the mistake!

I know this kind of question has been asked several times but none of the answers worked for me. I want to make my own spell-checker which pares words letter by letter and checks the correctness. As user types letters one by one, the content is checked. If it is correct, goes on, if it is wrong: I want the last letter to be deleted.

Here is my code:

var word = [];
word = "beautiful".split('');
var i =0;

$(document).ready(
function() 
{ $('#txtword').keyup(
 function() {
    var value = document.getElementById('txtword').value;
    if (value[i] == word[i]){ 
            alert("Right letter!");
            i++; }
            else
            { alert("Wrong letter");
            value.slice(0,-1);
            /*value.substr(0,value.length-1);*/
            }
        });
    })

I tried both options value.slice(0,-1); and value.substr(0,value.length-1); but they are not working. Can someone help me find the mistake!

Share Improve this question asked Mar 18, 2014 at 12:58 user2039789user2039789 972 silver badges11 bronze badges 1
  • 2 You're not assigning the value to anything. – Prinzhorn Commented Mar 18, 2014 at 12:59
Add a ment  | 

1 Answer 1

Reset to default 7

You need to assign new value back to value property of the element

this.value = value.substr(0,value.length-1);

Note: You can use this.value instead of document.getElementById('txtword').value;

发布评论

评论列表(0)

  1. 暂无评论