Javascript Ajax表单在包含jcarousel脚本时提交两次
我正在使用Ajax提交表单,但当我单击“添加到购物车”按钮时,表单将提交两次,但如果我删除jcarousel,表单将像正常一样提交Javascript Ajax表单在包含jcarousel脚本时提交两次,javascript,ajax,jquery,Javascript,Ajax,Jquery,我正在使用Ajax提交表单,但当我单击“添加到购物车”按钮时,表单将提交两次,但如果我删除jcarousel,表单将像正常一样提交 <form id="formrandom1473" name="formrandom1473" method="post" action="ajax_process.php?case=add_product_ajax&pid=1473"> <input type="hidden" value="1473" name="prod
<form id="formrandom1473" name="formrandom1473" method="post" action="ajax_process.php?case=add_product_ajax&pid=1473">
<input type="hidden" value="1473" name="products_id">
<input type="hidden" value="1" name="qty">
<div class="add_to_cart_div">
<input type="image" border="0" id="add-to-cart-1473" title=" Add to Cart " alt="Add to Cart" src="includes/languages/english/images/buttons/button_in_cart.gif">
</div>
</form>
<script type="text/javascript">
$(document).ready(function() {
$("#add-to-cart-1473").click(function () {
var formrandom1473 = $("#formrandom1473");
formrandom1473.submit(function () {
$.ajax({
type: formrandom1473.attr("method"),
url: formrandom1473.attr("action"),
data: formrandom1473.serialize(),
success: function (data) {
$("#output").html(data);
$(".message_div").addClass("message_success").slideDown().delay(6000).slideUp(); // Div success
$(".cart_counter").load("ajax_process.php?case=cart_counter"); // Load new quantity / by quantity
$(".shopping_cart_box").load("ajax_process.php?case=shopping_cart_box"); // Load new shopping cart box / by product
$(".cart_dropdown").load("ajax_process.php?case=cart_dropdown"); // Load new shopping cart box / by product
}
});
$("formrandom1473").unbind();
return false;
});
/* Effect */
var productX = $("#cart-image-1473").offset().left;
var productY = $("#cart-image-1473").offset().top;
var basketX = $("#boxcart-content").offset().left;
var basketY = $("#boxcart-content").offset().top;
var gotoX = basketX - productX;
var gotoY = basketY - productY;
var newImageWidth = $("#cart-image-1473").width() / 3;
var newImageHeight = $("#cart-image-1473").height() / 3;
$("#wrapper").html("");
$("#wrapper").css({"position":"absolute","top": productY,"left": productX});
$("#cart-image-1473").clone()
.prependTo("#wrapper")
.css({"position" : "absolute", "border" : "1px dashed black"})
.animate({opacity: 0.6})
.animate({opacity: 0.0, marginLeft: gotoX, marginTop: gotoY, width: newImageWidth, height: newImageHeight}, 1200,
function() {
});
/* Effect */
}); //click
}); //ready
</script>
<script type="text/javascript">
$(document).ready(function() {
$('.mycarousel').jcarousel();
});
</script>
$(文档).ready(函数(){
$(“#添加到购物车-1473”)。单击(函数(){
var formrandom1473=$(“#formrandom1473”);
表单1473.提交(函数(){
$.ajax({
类型:formrandom1473.attr(“方法”),
url:formrandom1473.attr(“操作”),
数据:formrandom1473.serialize(),
成功:功能(数据){
$(“#输出”).html(数据);
$(“.message_div”).addClass(“message_success”).slideDown().delay(6000.slideUp();//div success
$(“.cart\u counter”).load(“ajax\u process.php?case=cart\u counter”);//加载新数量/按数量加载
$(“.shopping\u cart\u box”).load(“ajax\u process.php?case=shopping\u cart\u box”);//按产品加载新的购物车box
$(“.cart\u dropdown”).load(“ajax\u process.php?case=cart\u dropdown”);//按产品加载新购物车框
}
});
$(“formrandom1473”).unbind();
返回false;
});
/*影响*/
var productX=$(“#cart-image-1473”).offset().左;
var productY=$(“#cart-image-1473”).offset().top;
var basketX=$(“#boxcart内容”).offset().左;
var basketY=$(“#boxcart内容”).offset().top;
var gotoX=篮子x-产品x;
var gotoY=篮筐-生产力;
var newImageWidth=$(“#cart-image-1473”).width()/3;
var newImageHeight=$(“#cart-image-1473”).height()/3;
$(“#包装器”).html(“”);
$(“#包装器”).css({“位置”:“绝对”,“顶部”:productY,“左侧”:productX});
$(“#cart-image-1473”).clone()
.prependTo(“包装器”)
.css({“位置”:“绝对”,“边框”:“1px黑色虚线”})
.animate({opacity:0.6})
.animate({opacity:0.0,marginLeft:gotoX,marginTop:gotoY,width:newImageWidth,height:newImageHeight},1200,
函数(){
});
/*效果*/
});//单击
}); //准备好的
$(文档).ready(函数(){
$('.mycarousel').jcarousel();
});
我尝试使用
$(“formrandom1473”).unbind()
但问题仍然是一样的。您是想使用$(“#formrandom1473”).unbind()吗用#
代替code>
或者,因为您之前已经存储了jQuery对象:
formrandom1473.unbind();
如果取消绑定对您不起作用,请尝试此操作,但将其移动到$之前。ajax
:
formrandom1473.submit(function() { return false; });
此外,请检查JS控制台是否有任何错误。请参阅本节。。在我使用e.stopImmediatePropagation()之后,表单最终可以像正常一样提交代码>
formrandom1473.submit(function (e) {
$.ajax({
type: formrandom1473.attr("method"),
url: formrandom1473.attr("action"),
data: formrandom1473.serialize(),
success: function (data) {
$("#output").html(data);
$(".message_div").addClass("message_success").slideDown().delay(6000).slideUp(); // Div success
$(".cart_counter").load("ajax_process.php?case=cart_counter"); // Load new quantity / by quantity
$(".shopping_cart_box").load("ajax_process.php?case=shopping_cart_box"); // Load new shopping cart box / by product
$(".cart_dropdown").load("ajax_process.php?case=cart_dropdown"); // Load new shopping cart box / by product
}
});
e.stopImmediatePropagation();
return false;