Trigger Keypress with jQuery

后端 未结 2 1268
情歌与酒
情歌与酒 2020-11-29 10:44

On the Definitive Trigger Keypress jQuery thread there is no working JSFiddle for the answer, and the code that is there doesn\'t work for me.

$(\"button\")         


        
2条回答
  •  广开言路
    2020-11-29 11:25

    Using trigger you are just triggering the event with a keycode but not assigning the value to the textbox. Try this :- http://jsfiddle.net/PbHD2/

    String.fromCharCode

    $("button").click(function() {
         $("input").focus();
        var e = jQuery.Event("keydown");
        e.which = 77; // # Some key code value
        $("input").val(String.fromCharCode(e.which));
        $("input").trigger(e);
    });
    $('input').keydown(function(e){
       console.log('Yes keydown triggered. ' + e.which)
    });
    

提交回复
热议问题