JQuery - 留言之后,不重新加载数据,直接显示发表内容

时间:2024-01-05 17:35:02

留言板中,发表信息的时候,使用Ajax存储到后台数据库,如果存储成功,不重新加载数据库,直接显示发表内容。

代码:

var Nicehng = '';
var kkimgpath = '';
var text = ''; $(function () {
if ($(document).scrollTop() != 0) {
//刷新之后,回到顶部
$('body,html').animate({ scrollTop: 0 }, 300);
}
//点击发表留言
$("#submit").click(function () {
editor.sync();
text = $.trim($('#KindEditor').val());//获取KindEditor的内容
if (text == "") {
art.dialog({ id: 'TS', width: 325, height: 170, content: '请输入内容!' });//显示提示文本框
}
else { Nicehng = $.trim($("#NiCheng").val());
if (Nicehng == "") {
Nicehng = '匿名';
}
var IgP = Imgpath();
kkimgpath = "../Images/demopage/image-" + IgP + ".jpg"; $.ajax({
type: "POST",
url: "Handeler/Ajax.ashx",
data: "NiCheng=" + Nicehng + "&Imgpath=" + kkimgpath + "&text=" + text + "",
success: function (msg) {
if (msg == "Null") {
art.dialog({ id: 'TS', width: 325, height: 170, content: '参数为空,请联系管理员!' });
}
if (msg == "True") {
PageBindData();//当数据库存储成功时,在前台显示发表内容。
//清空
$("#NiCheng").val();
$("#Radio1").attr("checked","checked");
$("#KindEditor").val();
//回到顶部
$('body,html').animate({ scrollTop: 0 }, 500);
}
if (msg == "False") {
art.dialog({ id: 'TS', width: 325, height: 170, content: '发表失败!' });
}
}
});
}
}) //获取Radio选中图片的值
function Imgpath() {
var val = $('input:radio[name="Img"]:checked').val();
if (val == null) {
return "";
}
else {
return val;
}
} function PageBindData() {
var mydate = new Date();
var t = mydate.toLocaleString();
var imgpath = kkimgpath;
var name = Nicehng;
var ip = '121.123.123.12';
var content = text;
var time = t;
//$("#newcontent").prepend("<div><ul><li><div><img src='" + kkimgpath + "'/></div></li><li><div><span>" + name + "</span></div></li><li><div><span>" + ip + "</span></div></li></ul></div><div><div>" + content + "</div><div><ul><li ><span >2楼</span></li><li ><span>" + time + "</span></li><li><a href='####' >回复</a></li></ul></div></div>"); $("#newcontent").prepend("<div class= 'messagecontent'><div class='maincontent'><ul><li><div class='smheadimg'><img class='img' src='" + kkimgpath + "'></div></li><li><div class='smname'><span class='smnamesm'>" + name + "</span</div></li><li><div class='smip'><span class='smipsm'>" + ip + "</span></div></li></ul></div><div class='cmcontent'><div class='cmcontentsm'>" + content + "</div><div class='reply'><ul class='replyul'><li><span class='floor'>2楼</span></li><li><span class='time'>" + time + "</span></li><li><a class='smreply' href='javascript:void(0)'>回复</a></li></ul></div> </div></div>");
}
})