这是我的JavaScript代码:
$(\"img.agree\").click(function()
{
var follow_id = $(this).attr(\"id\");
var user_id = $(\"img.user_id\").attr(\"id\");
$.ajax({
type: \"POST\",
url: \"/sys/follow.php\",
data: { 'follow_id': + follow_id, 'user_id': + user_id },
success: function(html){}
});
$(\"img.agree\"+follow_id).hide();
$(\"img.notagree\"+follow_id).css('display','inline-block');
});
$(\"img.notagree\").click(function()
{
var follow_id = $(this).attr(\"id\");
var user_id = $(\"img.user_id\").attr(\"id\");
$.ajax({
type: \"POST\",
url: \"/sys/dontfollow.php\",
data: { 'follow_id': + follow_id, 'user_id': + user_id },
success: function(html){}
});
$(\"img.notagree\"+follow_id).hide();
$(\"img.agree\"+follow_id).css('display','inline-block');
});
我在class='agree$string' or class='notagree$string' 之后只使用了一个字符串作为ajax请求,而不是发送等同于所有img标签数量的请求。但它没有正常运行起来,我认为问题出在+ follow_id或类似名称中....../p>
Have you considered moving this out into a javascript file? I don't see any point in generating this with PHP since there are no PHP variables being used. Also, it seems as if your HTML may look something like this.
<html>
<img id="1" class="agree1" src"" />
<img id="1" class="notagree1" src"" />
</html>
If that is the case you really shouldn't have different elements with the same ID. I think something like this.
<html>
<img id="agree1" class="agree" src"" />
<img id="notagree1" class="notagree" src"" />
</html>
The corresponding JS would be:
$("img.agree").click(function()
{
var follow_id = /\d/.exec(this.id); //parse out the number you are looking for with a regex
var user_id = $("img.user_id").attr("id");
$.ajax({
type: "POST",
url: "/sys/follow.php",
data: { 'follow_id': + follow_id, 'user_id': + user_id },
success: function(html){}
});
$("#agree"+follow_id).hide();
$("#notagree"+follow_id).css('display','inline-block');
});
$("img.notagree").click(function()
{
var follow_id = /\d/.exec(this.id); //parse out the number you are looking for with a regex
var user_id = $("img.user_id").attr("id");
$.ajax({
type: "POST",
url: "/sys/dontfollow.php",
data: { 'follow_id': + follow_id, 'user_id': + user_id },
success: function(html){}
});
$("#notagree"+follow_id).hide();
$("#agree"+follow_id).css('display','inline-block');
});
And again, I advocate removing this from the PHP as well