把按键变成一个按钮点击

Turning keypress into a button click

本文关键字:一个 按钮      更新时间:2023-09-26

我试图改变下面的代码,而不是按下一个键来激活它,我希望它激活时按下一个按钮。像大多数事情一样,我试着用谷歌来解决这个问题,但没有运气。所有的帮助是感激的,它一直困扰着我几个小时,谢谢!

$("#shout_message").keypress(function(evt) { if(evt.which == 13) { var iusername = $('#shout_username').val(); var imessage = $('#shout_message').val(); post_data = {'username':iusername, 'message':imessage};

使用jQuery点击事件我看到你正在使用jQuery所以让事件发生在按钮上点击遵循这个模式

$('#buttonID').click(function() {
        var iusername = $('#shout_username').val();
        var imessage = $('#shout_message').val();
        //Perform AJax POST call
        post_data = {'username':iusername, 'message':imessage};
            
            //send data to "shout.php" using jQuery $.post()
            $.post('shout.php', post_data, function(data) {
                
                //append data into messagebox with jQuery fade effect!
                $(data).hide().appendTo('.message_box').fadeIn();
                //keep scrolled to bottom of chat!
                var scrolltoh = $('.message_box')[0].scrollHeight;
                $('.message_box').scrollTop(scrolltoh);
                
                //reset value of message box
                $('#shout_message').val('');
                
            }).fail(function(err) { 
            
            //alert HTTP server error
            alert(err.statusText); 
            });
        
});

我修改了代码,这样你们就能看到它的实际效果了不过要再复习一下jquery你必须把完整的ajac请求放入onclick事件