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

javascript - How do I remove last character from input field on click? - Stack Overflow

programmeradmin6浏览0评论

I have a form that when buttons are clicked, it enters a value into an input field. I want to add another button that deletes the last character added. How would I acplish this using jQuery

I have a form that when buttons are clicked, it enters a value into an input field. I want to add another button that deletes the last character added. How would I acplish this using jQuery

Share Improve this question edited Sep 2, 2013 at 15:53 Cody Guldner 2,8961 gold badge26 silver badges36 bronze badges asked Aug 29, 2012 at 21:12 jenijeni 991 gold badge2 silver badges8 bronze badges 7
  • 5 what have you tried? can you post an simple example of the page on jsfiddle? – nathan gonzalez Commented Aug 29, 2012 at 21:13
  • do you mean a new character is attached at the end of your sting on each button click? – arjuncc Commented Aug 29, 2012 at 21:19
  • referthis from the stack-overflow itself. You have a good example too. [1]: stackoverflow./questions/952924/… – arjuncc Commented Aug 29, 2012 at 21:23
  • so this is basically what I am doing so far.. jsfiddle/jeni/jfs4x – jeni Commented Aug 29, 2012 at 21:34
  • Thanks for all the replies, I'm sure they all worked great but arjuncc made it really simple! – jeni Commented Aug 30, 2012 at 0:40
 |  Show 2 more ments

3 Answers 3

Reset to default 9
<script>
function addTextTag(txt)
{
document.getElementById("text_tag_input").value+=txt;
}
function removeTextTag()
{
var strng=document.getElementById("text_tag_input").value;
document.getElementById("text_tag_input").value=strng.substring(0,strng.length-1)
}
</script>

<input id="text_tag_input" type="text" name="tags" />

    <div class="tags_select">
    <a href="javascript:addTextTag('1')">1</a>
    <a href="javascript:addTextTag('2')">2</a>
    <a href="javascript:addTextTag('3')">3</a>
    <a href="javascript:removeTextTag()">delete</a>
</div>​

Used a modified version of your code itself try

the simple answer is, if you are using jquery, to do something like this:

//select the button, add a click event
$('#myButtonId').on('click',function () { 
    //get the input's value
    var textVal = $('#myInputId').val();
    //set the input's value
    $('#myInputId').val(textVal.substring(0,textVal.length - 1));
});
var lastChar = function (x) {
        "use strict";
        var a = document.getElementById(x),
            b = a.value;
        a.value = b.substring(0, b.length - 1);
    };

No jQuery required. The x variable is the id of the input you want to mutilate.

发布评论

评论列表(0)

  1. 暂无评论