我目前正在开发一个项目,我想点击一个对象来改变一个类,然后让下一次点击依赖于同一个jquery。
我只能在第一次工作 - 之后它将无法工作。我的来源如下:
$(document).ready(function(){
$("a.activate").click(function(){
var elmid = $(this).attr('id');
$.ajax({
type: "POST",
dataType: "json",
url: "/advertisement/activate", // URL of the Perl script
data: {adID: $(this).prev("input.ad_ID").val()},
success: function(data){
if (data.error) {
$('div#create_createresult').text(data.msg);
$('div#create_createresult').addClass("text-danger");
$("form#createForm input#createForm_submit").removeAttr('disabled');
}
else
{
var src = $("a#" + elmid + " img").attr("src").replace("activate-16x16.png", "deactivate-16x16.png");
$("a#" + elmid + " img").attr("src", src);
$("a#" + elmid).removeClass('activate').addClass('deactivate');
$('div#create_createresult').text(data.msg);
$('div#create_createresult').addClass("success");
}
}
});
});
$("a.deactivate").click(function(){
var elmid = $(this).attr('id');
$.ajax({
type: "POST",
dataType: "json",
url: "/advertisement/deactivate", // URL of the Perl script
data: {adID: $(this).prev("input.ad_ID").val()},
success: function(data){
if (data.error) {
$('div#create_createresult').text(data.msg);
$('div#create_createresult').addClass("text-danger");
$("form#createForm input#createForm_submit").removeAttr('disabled');
}
else
{
var src = $("a#" + elmid + " img").attr("src").replace("deactivate-16x16.png", "activate-16x16.png");
$("a#" + elmid + " img").attr("src", src);
$("a#" + elmid).removeClass('deactivate').addClass('activate');
$('div#create_createresult').text(data.msg);
$('div#create_createresult').addClass("success");
}
}
});
});
});
$(document).ready(function(){
$("a.activate").on( 'click', function(){
var elmid = $(this).attr('id');
$.ajax({
type: "POST",
dataType: "json",
url: "/advertisement/activate", // URL of the Perl script
data: {adID: $(this).prev("input.ad_ID").val()},
success: function(data){
if (data.error) { // script returned error
alert("fejl");
$('div#create_createresult').text(data.msg);
$('div#create_createresult').addClass("text-danger");
$("form#createForm input#createForm_submit").removeAttr('disabled');
}
else
{
var src = $("a#" + elmid + " img").attr("src").replace("activate-16x16.png", "deactivate-16x16.png");
$("a#" + elmid + " img").attr("src", src);
$("a#" + elmid).addClass('deactivate').removeClass('activate');
$('div#create_createresult').text(data.msg);
$('div#create_createresult').addClass("success");
}
}
});
});
$("a.deactivate").on ( 'click', function(){
var elmid = $(this).attr('id');
$.ajax({
type: "POST",
dataType: "json",
url: "/advertisement/deactivate", // URL of the Perl script
data: {adID: $(this).prev("input.ad_ID").val()},
success: function(data){
if (data.error) {
alert("fejl");
$('div#create_createresult').text(data.msg);
$('div#create_createresult').addClass("text-danger");
$("form#createForm input#createForm_submit").removeAttr('disabled');
}
else
{
var src = $("a#" + elmid + " img").attr("src").replace("deactivate-16x16.png", "activate-16x16.png");
$("a#" + elmid + " img").attr("src", src);
$("a#" + elmid).addClass('activate').removeClass('deactivate');
$('div#create_createresult').text(data.msg);
$('div#create_createresult').addClass("success");
}
}
});
});
});
答案 0 :(得分:0)
感谢杰森,芭芭拉和j08691,我提出了这个解决方案,如果你遇到同样的问题,我应该适合。
请注意$(document).ready()部分已删除!
$(document).on( 'click', "a.activate", function(){
var elmid = $(this).attr('id');
$.ajax({
type: "POST",
dataType: "json",
url: "/advertisement/activate", // URL of the Perl script
data: {adID: $(this).prev("input.ad_ID").val()},
success: function(data){
if (data.error) { // script returned error
alert("fejl");
$('div#create_createresult').text(data.msg);
$('div#create_createresult').addClass("text-danger");
$("form#createForm input#createForm_submit").removeAttr('disabled');
}
else
{
var src = $("a#" + elmid + " img").attr("src").replace("activate-16x16.png", "deactivate-16x16.png");
$("a#" + elmid + " img").attr("src", src);
$("a#" + elmid).addClass('deactivate').removeClass('activate');
$('div#create_createresult').text(data.msg);
$('div#create_createresult').addClass("success");
}
}
});
});
$(document).on ( 'click', "a.deactivate", function(){
var elmid = $(this).attr('id');
$.ajax({
type: "POST",
dataType: "json",
url: "/advertisement/deactivate", // URL of the Perl script
data: {adID: $(this).prev("input.ad_ID").val()},
success: function(data){
if (data.error) {
alert("fejl");
$('div#create_createresult').text(data.msg);
$('div#create_createresult').addClass("text-danger");
$("form#createForm input#createForm_submit").removeAttr('disabled');
}
else
{
var src = $("a#" + elmid + " img").attr("src").replace("deactivate-16x16.png", "activate-16x16.png");
$("a#" + elmid + " img").attr("src", src);
$("a#" + elmid).addClass('activate').removeClass('deactivate');
$('div#create_createresult').text(data.msg);
$('div#create_createresult').addClass("success");
}
}
});
});