How can I “undo” the programmatic insertion of text into a textarea?

后端 未结 12 654
失恋的感觉
失恋的感觉 2020-12-17 09:27

I have a textarea and a button. Clicking the button causes text to be inserted into the textarea.

Is there a way to allow a user to press Ctrl/Cmd+z to undo the inse

12条回答
  •  时光取名叫无心
    2020-12-17 10:02

    Add html first and you can use keypress event for undo

    you can try here also here http://jsfiddle.net/surendra786/1v5jxaa0/

                
    
                
    
                
    ADD
    UNDO

    **then add jquery **

        var items = [];
    
    $("#add").click(function() {
        // Push the new actor in the array
        items.push($("[name='actor']").val());
        populate();
    });
    
    
    
    $(document).keydown(function(e){
           if( e.which === 90 && e.ctrlKey ){
             console.log('control + z'); 
             if (items.length > 0) {
            // remove last element of the array
            items.splice(-1,1);
            populate();
        }
          }          
    }); 
    
    populate = function() {
        $("[name='actors-list']").text('');
        $("[name='actors-list']").append(items.join('
    '));
        $("[name='actors']").val(items.join(','));   
    }
    

    you can try here http://jsfiddle.net/surendra786/1v5jxaa0/

    this is working for me

提交回复
热议问题