日期:2014-05-16  浏览次数:20373 次

为什么用ajax传值后画面还是刷新呢
JScript code
$("#send").click(function() {
                            var msg = $("#msg").val();
                            var user;
                            if($("#user").val()==""){
                                user="游客";
                            }else{
                                user = $("#user").val();
                            }
                            $.ajax({
                                type : "post",
                                url : "chat_add",
                                data : {
                                    msg : msg,
                                    user : user
                                },
                                dataType : "json",
                                success : function(jsonStr) {
                                
                                    var data = eval(jsonStr);
                                    $
                                            .each(
                                                    data,
                                                    function(i,
                                                            n) {
                                                        msgchat += n.time
                                                        + " "
                                                        + n.user
                                                        + "&#58"
                                                        + "<br>"
                                                        + "&nbsp;&nbsp;&nbsp;&nbsp;"
                                                        + n.msg
                                                        + "<br>";
                                                    });
                                    document
                                            .getElementById('showMsg').innerHTML = msgchat;
                                    document
                                            .getElementById('showMsg').scrollTop = document
                                            .getElementById('showMsg').scrollHeight;
                                }
                            });
                            $("#msg").val("");//清空
                        });

Java code
    public String add() throws Exception  {
        chat.setMsg(msg);
        chat.setUser(user);
        this.chatService.addMsg(chat);
        this.msglist=this.chatService.searchMsg(chat);//查询
        outputJson(msglist);
        return null;
    }
每次发送完信息后画面总是刷新

------解决方案--------------------
$("#send"),你这个send类型不会是submit吧而且放到表单里面?这样会提交表单就会刷新了

JScript code
$("#msg").val("");//清空
return false;///////////增加这句阻止表单提交