Jquery混乱-为什么我的超链接停止工作
我发现有人的投票系统使用jquery,并希望将其整合到我的网站中。不幸的是,我想让它检测用户是否已经投票,并基于此显示向上投票或向下投票按钮(和链接) 它工作正常-除了动态更改投票按钮外,例如,如果我投票否决某个项目,我会将图标更改为向上投票,但当我单击向上投票图标时,不会触发超链接操作。我需要做些什么来“重新连接”吗?注意:我刚刚实现了投票否决、投票否决的逻辑,我还没有改变,所以它现在清除了投票按钮。这将是固定的 在这种情况下,了解这一点很重要-这是在.net/mvc应用程序中Jquery混乱-为什么我的超链接停止工作,jquery,Jquery,我发现有人的投票系统使用jquery,并希望将其整合到我的网站中。不幸的是,我想让它检测用户是否已经投票,并基于此显示向上投票或向下投票按钮(和链接) 它工作正常-除了动态更改投票按钮外,例如,如果我投票否决某个项目,我会将图标更改为向上投票,但当我单击向上投票图标时,不会触发超链接操作。我需要做些什么来“重新连接”吗?注意:我刚刚实现了投票否决、投票否决的逻辑,我还没有改变,所以它现在清除了投票按钮。这将是固定的 在这种情况下,了解这一点很重要-这是在.net/mvc应用程序中 <sc
<script type="text/javascript">
$(document).ready(function() {
function onChange(newPageValue) {
UpdateStories(newPageValue);
}
$(function() {
$("a.vote_up").click(function() {
//get the id
var theId = $(this).attr('id').replace('vote_', '');
// show the spinner
$(this).parent().html("<img src='content/images/spinner.gif'/>");
//fadeout the vote-count
$("span#votes_count_" + theId).fadeOut("fast");
//the main ajax request
$.ajax({
type: "POST",
data: { action: "vote_up", id: $(this).attr("id")},
url: "@Url.Action("ProcessVote", "Vote")",
success: function(msg) {
$("span#votes_count_" + theId).html(msg);
// fadein the vote count
$("span#votes_count_" + theId).fadeIn();
// remove the spinner
$("span#vote_buttons_" + theId).html('');
}
});
});
});
$(function() {
$("a.vote_down").click(function() {
//get the id
var theId = $(this).attr('id').replace('vote_', '');
// show the spinner
$(this).parent().html("<img src='content/images/spinner.gif'/>");
//fadeout the vote-count
$("span#votes_count_" + theId).fadeOut("fast");
//the main ajax request
$.ajax({
type: "POST",
data: { action: "vote_down", id: $(this).attr("id")},
url: "@Url.Action("ProcessVote", "Vote")",
success: function(msg) {
$("span#votes_count_" + theId).html(msg);
// fadein the vote count
$("span#votes_count_" + theId).fadeIn();
// remove the spinner
var votelink = "<a href='javascript:;' class='vote_up' id='vote_" + theId + "'></a>";
$("span#vote_buttons_" + theId).html(votelink);
}
});
});
});
});
</script>
$(文档).ready(函数(){
函数onChange(newPageValue){
UpdateStores(newPageValue);
}
$(函数(){
$(“a.vote_up”)。单击(函数(){
//拿到身份证
var theId=$(this.attr('id').replace('vote_uu','');
//展示旋转器
$(this.parent().html(“”);
//淡出计票
$(“span#voces#u count#”+theId)。淡出(“快速”);
//主要的ajax请求
$.ajax({
类型:“POST”,
数据:{操作:“投票”,id:$(this.attr(“id”)},
url:“@url.Action(“ProcessVote”、“Vote”)”,
成功:功能(msg){
$(“span#vows u count u”+theId).html(msg);
//计票
$(“span#voces_count_u”+theId).fadeIn();
//卸下旋转器
$(“投票按钮”+theId).html(“”);
}
});
});
});
$(函数(){
$(“a.vote_down”)。单击(函数(){
//拿到身份证
var theId=$(this.attr('id').replace('vote_uu','');
//展示旋转器
$(this.parent().html(“”);
//淡出计票
$(“span#voces#u count#”+theId)。淡出(“快速”);
//主要的ajax请求
$.ajax({
类型:“POST”,
数据:{action:“vote_down”,id:$(this.attr(“id”)},
url:“@url.Action(“ProcessVote”、“Vote”)”,
成功:功能(msg){
$(“span#vows u count u”+theId).html(msg);
//计票
$(“span#voces_count_u”+theId).fadeIn();
//卸下旋转器
var votelink=“”;
$(“投票按钮”+theId).html(votelink);
}
});
});
});
});
html/mvc视图中引用此内容的部分是:
@foreach (var story in Model.UserStories)
{
<tr>
<td>@story["FormattedID"]
</td>
<td>@story["Name"]
</td>
<td>@Html.Raw(story["Description"])
</td>
<td>@story["TaskEstimateTotal"]
</td>
<td>
<span class='votes_count' id='votes_count_@story["FormattedID"]'>@story["VoteCount"]</span> votes
<br/>
<span class='vote_buttons' id='vote_buttons_@story["FormattedID"]'>
@if (story["Voted"])
{
<a href='javascript:;' class='vote_down' id='vote_@story["FormattedID"]'></a>
}
else
{
<a href='javascript:;' class='vote_up' id='vote_@story["FormattedID"]'></a>
}
</span>
</td>
</tr>
}
@foreach(Model.UserStories中的var故事)
{
@故事[“格式化ID”]
@故事[“名字”]
@Html.Raw(故事[“描述”])
@故事[“任务估计总计”]
@故事[“VoteCount”]投票
@如果(故事[“投票”])
{
}
其他的
{
}
}
所以我的逻辑正常工作——除了当我动态地将投票按钮放入html占位符时,它不再触发它需要的事件
编辑-我确实尝试将函数移到document ready函数之外,但这也不能解决问题。这将只注册页面上当前元素的单击事件
$("a.vote_up").click(function() {
如果要确保覆盖动态元素,您应该做的是在上使用
$("body").on("click",'a.vote_up',function(){
这将把这次活动委托给所有当前和未来的主播元素,并进行全班投票。非常感谢-这很有效:)(很高兴能得到解释原因!)