添加新帖子后,Ajax无法正常工作

时间:2016-04-06 10:09:36

标签: php jquery ajax

<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script type="text/javascript">
    $(document).ready(function() {

        $('#message').focus();
        $('#message').keypress(function(event) {

            var key = (event.keyCode ? event.keyCode : event.which);
            if (key == 13) {
                var message = $('#message').val();
                var sender = $($sender).val();
                var reciver = $($reciver).val();
                $.ajax({
                    method: "POST",
                    url: "code.php",
                    data: {message: message, sender: sender, reciver: reciver},
                    success: function(status) {
                        $('#result').append(status);
                        $('#message').val('');
                    }
                });
            };
        });
    });
</script>

当我添加这些代码时,我的“输入”无效,但是当我删除它时,我有一点问题。我做错了什么?

sender: sender, reciver: reciver
var sender = $($sender).val();
var reciver = $($reciver).val();

顺便说一下$ sender和$ reciver是GET函数

1 个答案:

答案 0 :(得分:0)

试试这个:

  $(document).ready(function() {

        $('#message').focus();
        $('#message').keypress(function(event) {

            var key = (event.keyCode ? event.keyCode : event.which);
            if (key == 13) {
                var message = $('#message').val();
                var sender = '<?php echo $sender;?>';
                var reciver = '<?php echo $reciver;?>';
                $.ajax({
                    method: "POST",
                    url: "code.php",
                    data: {message: message, sender: sender, reciver: reciver},
                    success: function(status) {
                        $('#result').append(status);
                        $('#message').val('');
                    }
                });
            };
        });
    });