gpt4 book ai didi

javascript - 为什么这个 Ajax 条件对话框不起作用?

转载 作者:行者123 更新时间:2023-11-28 10:02:44 25 4
gpt4 key购买 nike

为什么这段代码不起作用?

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

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

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

这是工作流程:

If DialogRequired => Dialog.Click = OK --> 执行 ajax 调用如果 DialogRequired => Dialog.Click = Cancel --> 不执行任何操作如果不需要对话框 => 执行 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
  • 但是对话框没有被调用,而且我似乎无法使用 firebug 更新它。

最佳答案

您的范围似乎有问题。

在 ajax 成功的匿名函数中,您有很多 $(this).attr("href") 。在这些函数中 this != "a.orderlink".

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

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

关于javascript - 为什么这个 Ajax 条件对话框不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8929512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com