[英]jQuery after click event issue
I'm currently having problems with my C# web application. 我的C#Web应用程序当前有问题。 Here is my JavaScript:
这是我的JavaScript:
$(document).ready(function () {
$("#submitComment").click(function () {
$.post("/Home/Index", { "CommentText": $("#CommentText").val() }, function (Comment) {
for (var index = 0; index < Comment.length; index++) {
var dateObject = eval('new' + Comment[index].CommentDate.replace(/\//g, ' '));
Comment[index].CommentDate = cleanDate(dateObject);
}
$("#commentList li").not("li:last").replaceWith($("#commentTemplate").tmpl(Comment));
for (var newIndex = 0; newIndex < Comment.length; newIndex++) {
addLikeToComment(Comment[newIndex].ID);
}
});
});
function cleanDate(dateObject) {
var Month = dateObject.getMonth();
var Day = dateObject.getDate();
var month = new Array("Janúar", "Febrúar", "Mars", "Apríl", "Maí", "Júní", "Júlí", "Ágúst", "September", "Október", "Nóvember", "Desember");
var Hour = dateObject.getHours();
var Minute = dateObject.getMinutes();
if (Hour < 10)
Hour = "0" + Hour;
if (Minute < 10)
Minute = "0" + Minute;
var dateString = (Day + ". " + month[Month] + " " + Hour + ":" + Minute);
return dateString;
}
function addLikeToComment(ID) {
$.get("Home/GetLikeByID/" + ID, "", function (Likes) {
if (Likes.length != 0) {
var selector = "div #" + ID;
$("#likeTemplate").tmpl(Likes).appendTo($(selector));
}
});
}
$("#commentList li div.commentBody div a").click(function (event) {
var id = $(this).find("span").html();
id = id.replace(/^\s+|\s+$/g, "");
$.post("/Home/AddLike/" + id, "", function (LikeList) {
if (LikeList.length != 0 || LikeList[0].UserName != "") {
var selector = "div #" + id;
$(selector).empty();
$("#likeTemplate").tmpl(LikeList).appendTo($(selector));
}
});
event.preventDefault();
});});
And the templates: 和模板:
<script id="commentTemplate" type="text/html">
<li>
<a href="http://www.ru.is"><img src='<%= Url.Content("~/Images/User.png") %>' alt="Notandi" /></a>
<div class="commentBody">
<a href="http://www.ru.is">${UserName}</a>
<span>${CommentText}</span>
<div>
<abbr title='${CommentDate}'>${CommentDate}</abbr>
<a href="#"><span style="display:none;"> ${ID} </span>Líkar þetta</a>
<div id="${ID}" class="likeList">
</div>
</div>
</div>
</li>
</script>
<script id="likeTemplate" type="text/html">
<p class="likeLook"> ${UserName} Líkar þetta.</p>
</script>
Both works independently but if I click the "#submitComment" and then click "#commentList li div.commentBody div a" then that event doesn't fire, ie I can create a comment but after that I can't "Like" anything. 两者都可以独立工作,但是如果我单击“ #submitComment”,然后单击“ #commentList li div.commentBody div a”,则该事件不会触发,即,我可以创建评论,但之后就不能“喜欢”任何东西。 Does anyone know what the problem is?
有人知道问题出在哪里吗?
Its hard to say exactly but it looks to me like you are manipulating #commentList
on callback of your $.post
. 很难确切地说,但是在我看来,您好像在
$.post
回调上操纵#commentList
。 If thats the case its likely that you are just losing your click handler on the object itself. 如果是这样的话,很可能您只是失去了对象本身的点击处理程序。
A simple test would be to change 一个简单的测试就是改变
$("#commentList li div.commentBody div a").click(function(event){...
to 至
$("#commentList li div.commentBody div a").live('click',function(event){...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.