jQuery-添加到收藏夹
我有很多报告。每个报告都作为自己的页面。当用户在页面上时,我希望他们能够将报告添加到他们的收藏夹中。我不是指浏览器收藏夹,我是指他们登录系统时最喜欢的报告 出于显而易见的原因,谷歌搜索这篇文章会带来无数关于如何添加到浏览器收藏夹的教程/脚本,这不是我想要的 我有一个按钮,上面写着“添加到收藏夹”。单击此按钮时,应添加报告。然后应移除该按钮,并将其替换为“从收藏夹中移除”按钮 我意识到实际的添加/删除需要ASP/PHP,但任何关于如何实现这一点的指导都会非常有用 会不会是类似于jQuery-添加到收藏夹,jquery,post,jquery-post,jquery-click-event,Jquery,Post,Jquery Post,Jquery Click Event,我有很多报告。每个报告都作为自己的页面。当用户在页面上时,我希望他们能够将报告添加到他们的收藏夹中。我不是指浏览器收藏夹,我是指他们登录系统时最喜欢的报告 出于显而易见的原因,谷歌搜索这篇文章会带来无数关于如何添加到浏览器收藏夹的教程/脚本,这不是我想要的 我有一个按钮,上面写着“添加到收藏夹”。单击此按钮时,应添加报告。然后应移除该按钮,并将其替换为“从收藏夹中移除”按钮 我意识到实际的添加/删除需要ASP/PHP,但任何关于如何实现这一点的指导都会非常有用 会不会是类似于 $(functio
$(function() {
$('.report_add').click(function() {
this_id= this.attr("id");
$.ajax({
type: 'POST',
data: 'reportid'+this_id+'=&userid=789',
success: function() { ... },
error: function(){ ... },
url: '/url/',
cache:false
});
});
});
您可以更改此设置:
this_id= this.attr("id");
data: 'reportid'+this_id+'=&userid=789',
为此:
var this_id= this.id;
data: 'reportid='+this_id+'&userid=789',
或:
在代码中有两个问题 1。您没有正确选择id,因为您将jQuery的
.attr()
方法应用于dom节点而不是jQuery对象。所以必须是this.id
或$(this.attr('id')
2.您的数据字符串格式不正确:
data: 'reportid'+this_id+'=&userid=789',
//-------------^----------^--------------your '=' sign is at wrong place
相反,您可以发送如下值:
data: 'reportid='+this_id+'&userid=789',
或
在代码中:
$(function() {
$('.report_add').click(function() {
var this_id= this.id; // <---------update this
$.ajax({
type: 'POST',
data: {reportid : this_id, userid : 789}, // <---- and this
success: function() { ... },
error: function(){ ... },
url: '/url/',
cache:false
});
});
});
$(函数(){
$('.report_add')。单击(函数(){
var this_id=this.id;//试试这个
$(function() {
$('.report_add').click(function() {
this_id= $(this).attr("id");
$.ajax({
type: 'POST',
data: 'reportid'+this_id+'&userid=789',
success: function() {
$(this).text('Remove From Favourite');
},
error: function(){ ... },
url: '/url/',
cache:false
});
});
});
我不确定这个问题的目的是什么,因为没有任何服务器端代码来调用AJAX是毫无意义的。使用$(this.attr('id')而不是this.attr('id')来确保jquery工作properly@RoryMcCrossan-我知道数据发布到PHP后要做什么/ASP@C-链接我认为是的,它可能会产生问题,似乎是一个gloabal变量。
$(function() {
$('.report_add').click(function() {
var this_id= this.id; // <---------update this
$.ajax({
type: 'POST',
data: {reportid : this_id, userid : 789}, // <---- and this
success: function() { ... },
error: function(){ ... },
url: '/url/',
cache:false
});
});
});
$(function() {
$('.report_add').click(function() {
this_id= $(this).attr("id");
$.ajax({
type: 'POST',
data: 'reportid'+this_id+'&userid=789',
success: function() {
$(this).text('Remove From Favourite');
},
error: function(){ ... },
url: '/url/',
cache:false
});
});
});