使用ajax发送信息的问题

时间:2012-03-16 22:40:23

标签: php javascript ajax post

我制作了一个审核剧本,在问题和答案中上下投票。前一段时间脚本工作了,但是当我再次开始在这个项目中编码时,它再也不起作用了。

脚本正在做:当你点击一个叫做投票的div或者投票1一个链接时,它会询问它是上升还是下降。如果它已启动,则会加载url:“mod_up_vote.php”,通过邮寄方式发送有关您投票的问题的一些信息。还可以。但是不是现在。它没有加载该页面,因为我在数据库中打印并插入了$ _POST变量,并且没有任何内容。

你觉得这里有什么问题?感谢。

   $(document).ready(function()
   {

   $(document).delegate('.vote, .vote1', '.vote2', 'click', function()
       {

   var id = $(this).attr("id");
   var name = $(this).attr("name");
   var dataString = 'id='+ id ;
   var parent = $(this);

   if(name=='mod_up')
   {

   $(this).fadeIn(200).html('<img src="dot.gif" align="absmiddle">');
   $.ajax({
      type: "POST",
      url: "mod_up_vote.php",
      dataType: "xml",
      data: dataString,
      cache: false,

      success: function(xml)
      {
   //$("#mod-pregunta").html(html);
   //$("#mod-respuesta").html(html);

   //parent.html(html);
    $(xml).find('pregunta').each(function(){
    var id = $(this).attr('id');
    var pregunta = $(this).find('preguntadato').text();
    var respuesta = $(this).find('respuestadato').text();
    var votoup = $(this).find('votoup').text();
    var votodown = $(this).find('votodown').text();
    var id_pregunta = $(this).find('id_pregunta').text();
    var id_respuesta = $(this).find('id_respuesta').text();
    $("#mod-pregunta").html(pregunta);
    $("#mod-respuesta").html(respuesta);
    //$(".vote").attr('id', $(this).find('id_pregunta').text());
    $(".vote1").html("<a href=\"modera\" title=\""+ id_pregunta + "-"+ id_respuesta + "-1\" class=\"vote1\" id=\""+ id_pregunta + "-"+ id_respuesta + "-1\" name=\"mod_up\">Aceptar</a>");
    $(".vote2").html("<a href=\"modera\" title=\""+ id_pregunta + "-"+ id_respuesta + "-2\" class=\"vote2\" id=\""+ id_pregunta + "-"+ id_respuesta + "-2\" name=\"mod_up\">Rechazar</a>");

    //$("span", this).html("(ID = '<b>" + this.id + "</b>')");
     });
     }  });

   }
 return false;
});

1 个答案:

答案 0 :(得分:1)

这看起来像是你的JQuery的一个问题。为什么使用delegate代替click?此外,您对委托的参数似乎不正确。如果你查看文档,你会看到函数签名是:

$(elements).delegate(selector, events, data, handler);  // jQuery 1.4.3+

您正在将您的函数绑定到名为'.vote2'的事件,我只能认为该事件不存在。

尝试使用click代替,我没有理由使用委托。

编辑:

尝试使用click,如下所示:

$('.vote, .vote1').click(function(){ /* your code here */ });