【发布时间】:2010-03-15 00:23:58
【问题描述】:
因此,在我的网站上,用户可以对两件事发表评论:用户的个人资料和应用。该代码在 PHP 中运行良好,但我们决定添加 Ajax 以使其更时尚。评论只是淡入页面并且工作正常。
我决定创建一个函数,这样我就不必管理不同文件中的 2 个(或更多)代码块。现在,两个页面的代码如下(不是在单独的 .js 文件中,它们写在页面的 head 标签内。):
// App page
$("input#comment_submit").click(function() {
var comment = $("#comment_box").val();
$.ajax({
type: "POST",
url: "app.php?id=<?php echo $id; ?>",
data: {comment: comment},
success: function() {
$("input#comment_submit").attr("disabled", "disabled").val("Comment Submitted!");
$("textarea#comment_box").attr("disabled", "disabled")
$("#comments").prepend("<div class=\"comment new\"></div>");
$(".new").prepend("<a href=\"profile.php?username=<?php echo $_SESSION['username']; ?>\" class=\"commentname\"><?php echo $_SESSION['username']; ?></a><p class=\"commentdate\"><?php echo date("M. d, Y", time()) ?> - <?php echo date("g:i A", time()); ?></p><p class=\"commentpost\">" + comment + "</p>").hide().fadeIn(1000);
}
});
return false;
});
接下来,
// Profile page
$("input#comment_submit").click(function() {
var comment = $("#comment_box").val();
$.ajax({
type: "POST",
url: "profile.php?username=<?php echo $user; ?>",
data: {comment: comment},
success: function() {
$("input#comment_submit").attr("disabled", "disabled").val("Comment Submitted!");
$("textarea#comment_box").attr("disabled", "disabled")
$("#comments").prepend("<div class=\"comment new\"></div>");
$(".new").prepend("<a href=\"profile.php?username=<?php echo $_SESSION['username']; ?>\" class=\"commentname\"><?php echo $_SESSION['username']; ?></a><p class=\"commentdate\"><?php echo date("M. d, Y", time()) ?> - <?php echo date("g:i A", time()); ?></p><p class=\"commentpost\">" + comment + "</p>").hide().fadeIn(1000);
}
});
return false;
});
现在,在每个页面上,框名称将始终相同(comment_box 和comment_submit),那么你们如何看待这个功能(注意,postComment 在页面的 head 标签中。):
// On the page, (profile.php)
$(function() {
$("input#comment_submit").click(function() {
postComment("profile", "<?php echo $user ?>", "<?php echo $_SESSION['username']; ?>", "<?php echo date("M. d, Y", time()) ?>", "<?php echo date("g:i A", time()); ?>");
});
});
这导致了这个函数(它存储在一个名为functions.js的单独文件中):
function postComment(page, argvalue, username, date, time) {
if (page == "app") { var arg = "id"; }
if (page == "profile") { var arg = "username"; }
var comment = $("#comment_box").val();
$.ajax({
type: "POST",
url: page + ".php?" + arg + "=" + argvalue,
data: {comment: comment},
success: function() {
$("textarea#comment_box").attr("disabled", "disabled")
$("input#comment_submit").attr("disabled", "disabled").val("Comment Submitted!");
$("#comments").prepend("<div class=\"comment new\"></div>");
$(".new").prepend("<a href=\"" + page + ".php?" + arg + "=" + username + "\" class=\"commentname\">" + username + "</a><p class=\"commentdate\">" + date + " - " + time + "</p><p class=\"commentpost\">" + nl2br(comment) + "</p>").hide().fadeIn(1000);
}
});
return false;
}
这就是我想出的!所以,一些问题:当我点击按钮时页面刷新。解决此问题的方法是从函数中返回 false 并将其放入按钮单击中。有什么办法可以把它保留在函数中并具有相同的效果?
但我真正的问题是: 任何熟悉 jQuery 的编码人员可以告诉我技术、编码实践或更高效/优雅地编写代码的方法吗?我在 PHP 中做了很多工作,但我知道回显日期可能不是获取日期和时间的最有效方式。
因此,非常欢迎任何可以真正帮助我简化此功能并让我更好地编写 jQuery 的技巧!
【问题讨论】:
标签: ajax coding-style function jquery