AJAX未在对话框按钮上运行按钮单击

时间:2012-05-21 14:36:31

标签: javascript ajax alertdialog

我想知道是否有人可以帮助我。

首先,道歉,因为我刚刚开始使用Javascript,所以我可能是在犯一个非常基本的错误,所以请耐心等待。

我正在尝试在显示this的图库页面中的here第三方软件中实施Example 2

在下面的代码中显示,我已经能够将消息添加到onclick事件,即当用户单击“bin”图标时,但是应该在“删除”按钮上运行的AJAX代码片段被点击是失败的:

<script type="text/javascript"> 

    Galleria.ready(function() {
        this.$('thumblink').click();

    $(".galleria-image").append( 
    "<span class='btn-delete ui-icon ui-icon-trash'></span>"); 
    $(".btn-delete").live("click", function(){
    $.msgbox("You are about to delete this image. It cannot be restored at a later date. Do you wish to continue?", {
    type: "alert",
      buttons : [
        {type: "submit", value: "Delete"},
        {type: "cancel", value: "Cancel"}
      ]
      },function(Delete) {
      var img = $(this).closest(".galleria-image").find("img"); 
      // send the AJAX request
      $.ajax({
        url : 'delete.php',
        type : 'post',
        data : { image : img.attr('src'),  userid: "VALUE", locationid: "VALUE"},
        success : function(){
        img.parent().fadeOut('slow');
    }
    });               
    });
    return false;
    });     
    });

</script>

我真的不确定我在哪里出错了,尽管花了一些时间来处理代码。我只是想知道是否有人可以看看这个,让我知道我哪里出错了。

POST UPDATE 工作解决方案

<script type="text/javascript"> 

        Galleria.ready(function() {
            this.$('thumblink').click();

        $(".galleria-image").append( 
        "<span class='btn-delete ui-icon ui-icon-trash'></span>"); 
        $(".btn-delete").live("click", function(){  
        var img = $(this).closest(".galleria-image").find("img"); 
        $.msgbox("You are about to delete this image. It cannot be restored at a later date. Do you wish to continue?", {
        type: "alert",
          buttons : [
            {type: "submit", value: "Delete"},
            {type: "cancel", value: "Cancel"}
          ]
          },
          function(result) {
          if(result)

          // send the AJAX request
          $.ajax({
            url : 'delete.php',
            type : 'post',
            data : { image : img.attr('src'),  userid: "VALUE", locationid: "VALUE"},
            success : function(){
            img.parent().fadeOut('slow');

    }
        });               
        });
        return false;
        });     
        });

    </script>

非常感谢和问候

2 个答案:

答案 0 :(得分:0)

似乎msgbox回调中的this不是一个元素(换句话说,img变量是一个空数组。尝试将var img声明放入onclick听众。

$(".btn-delete").live("click", function(){
var img = $(this).closest(".galleria-image").find("img");

有帮助吗?

答案 1 :(得分:0)

我已将变量Delete重命名为result

function(result) {
  //result variable holds the value of what user clicks 'Delete' or 'Cancel'. 
  //Cancel click will pass false.
  //Delete click will pass Delete.
  alert(result)
  //You can also check if(result=='Delete') instead of if(result)
  if(result)
  {
      var img = $(this).closest(".galleria-image").find("img"); 
      // send the AJAX request
      $.ajax({
        url : 'delete.php',
        type : 'post',
        data : { image : img.attr('src'),  userid: "VALUE", locationid: "VALUE"},
        success : function(){
        img.parent().fadeOut('slow');
    }
  }

看看是否有效。