通过jQuery基于链接位置进行操作

如何检索给定链接的rel属性的值并将其传递给处理程序?

我有一个这样的链接列表

链接列表

Answer1 delete
Answer2 delete
Answer3 delete

这些链接由以下代码生成。 rel的以下值似乎没有传递给处理程序。

PHP生成的链接

  echo (https://stackoverflow.com/questions/1350539/to-make-an-action-based-on-the-link-location-by-jquery/$answer . "delete" ); 

用户单击第二个链接。 rel属性应该将答案传递给处理程序。 jQuery应该基于POST -data执行以下操作。

jQuery的

 jQuery('a.delete_answer').live('click', function(){ jQuery.post('/codes/handlers/delete_an_answer.php', { delete_answer: jQuery(this).attr('rel') }, function(){ https://stackoverflow.com/questions/1350539/to-make-an-action-based-on-the-link-location-by-jquery/$("#one_answer").removeClass("yellow"); }) }); 

文件delete_an_answer.php

 https://stackoverflow.com/questions/1350539/to-make-an-action-based-on-the-link-location-by-jquery/$dbconn = pg_connect("host=localhost port=5432 dbname=masi user=masi password=123"); // remove the answer https://stackoverflow.com/questions/1350539/to-make-an-action-based-on-the-link-location-by-jquery/$result = pg_query_params ( https://stackoverflow.com/questions/1350539/to-make-an-action-based-on-the-link-location-by-jquery/$dbconn, 'DELETE FROM answers WHERE answer = https://stackoverflow.com/questions/1350539/to-make-an-action-based-on-the-link-location-by-jquery/$1', array (https://stackoverflow.com/questions/1350539/to-make-an-action-based-on-the-link-location-by-jquery/$_POST['answer'] ) // Problem here, because // https://stackoverflow.com/questions/1350539/to-make-an-action-based-on-the-link-location-by-jquery/$_POST['answer'] is empty when it gets here // so no answer is deleted ); 

我从Postgres获取答案,同时将rel属性的值设置为https://stackoverflow.com/questions/1350539/to-make-an-action-based-on-the-link-location-by-jquery/$ body。

我可以看到这个代码有几个问题:

  1. 您有多个

    。 ID应该是唯一的,所以$("#one_answer").removeClass("yellow"); 将始终选择第一个div ,而不是所有div。

  2. delete_an_answer.php – 在php中你正在寻找$_POST['answer'] ,但是ajax有{question_id: jQuery(this).attr('rel')}
  3. 样本中的链接没有rel ,而href='$' ,这很奇怪。 此外

    至于你提出的其他问题,我真的不知道你在问什么。