2012-01-19 24 views
1

为什么此代码无法正常工作?为什么这个Ajax条件对话框不起作用?

与此同时(试图使它工作)我已经改变了它的时间,但我找不到解决方案。

任何人有想法?我在控制台中没有错误。

首先,它检查是否需要打开一个对话框。

这是工作流程:

如果DialogRequired => Dialog.Click = OK - >执行Ajax调用 如果DialogRequired => Dialog.Click =取消 - >无能为力 如果对话不要求= >执行一个Ajax调用

$(function() { 
    $("a.orderlink").unbind(); 

    $("a.orderlink").bind("click", function() { 
     var ProductID = $(this).parent().attr("data-productid"); 
     var Ammount = $(this).parent().parent().find("input#ammount").val(); 

     $.ajax({ type: "post", 
      url: $(this).attr("href").replace("AddToCart", "ExistsInCart"), 
      data: { ProductId: $(this).parent().attr("data-productid") }, 
      succes: function (data) { 
       if (data == 1) { 
        $("#ProductExistsInOrder").dialog({ 
         autoOpen: true, 
         height: 170, 
         width: 400, 
         modal: true, 
         buttons: { 
          "OK": function() { 
           /*acties om toe te voegen $.ajax()*/ 
           $.ajax({ type: "post", 
            url: $(this).attr("href"), 
            data: { ProductId: ProductID, Ammount: Ammount }, 
            succes: function() { 
             $("#AddProductNotification").text("U heeft net een product toegevoegd. Herlaad de pagina om uw winkelwagentje te bekijken"); 
            } 
           }); 
           setTimeout("location.reload(true);", 100); 
           $(this).dialog("close"); 
           location.reload(true); 
          // return false; 
          }, 
          "Annuleer": function() { 
           $(this).dialog("close"); 
          // return false; 
          } 
         } 
        }); 
       } else { 
        $.ajax({ type: "post", 
         url: $(this).attr("href"), 
         data: { ProductId: ProductID, Ammount: Ammount }, 
         succes: function() { 
          $("#AddProductNotification").text("U heeft net een product toegevoegd. Herlaad de pagina om uw winkelwagentje te bekijken"); 
         } 
        }); 


       }; 
       // $("#AddProductNotification").text("U heeft net een product toegevoegd. Herlaad de pagina om uw winkelwagentje te bekijken"); 
      }, 
      error: function (XMLHeeptRequest, textStatus, errorThrown) { 
       alert(textStatus); 
       alert(errorThrown); 
      } 
     }); 
     // alert("end"); 
     // AddToCart(this); 
     return false; 
    }); 
    // return false; 
}); 
// ProductId: $(orderlinkObject).parent().attr("data-productid"), Ammount: $(orderlinkObject).parent().parent().find("input#ammount").val() 

这是怎么一回事呢:

  • 获取调用(= ok):/ Cart/ExistsInCart带参数:产品ID并在jSon中返回true
  • 但该对话框未被调用,我似乎无法使用萤火虫进行更新。
+0

它会抛出任何错误吗? – Abadon

+2

什么不起作用?和什么工作? –

+1

请定义“不工作”。代码应该做什么,它实际上在做什么以及控制台上是否存在任何错误。 –

回答

0

看起来你有问题与你的范围。

你在AJAX成功的匿名函数中有很多$(this).attr("href")。在那些功能this != "a.orderlink"

您将需要在您的点击处理程序的顶部执行var that = $(this),然后使用that.attr("href")

例如:http://jsbin.com/ivoniv/edit#javascript

+0

更新了我的问题。由于巧合,我也在开始时将href更改为var。但是这并没有帮助我。 – NicoJuicy