我已经header.php文件,我已经代码做livesearch。在搜索中,我直接添加了"添加到购物车"按钮,这样用户就可以通过搜索直接添加产品。我在头文件中有购物车div,其中显示购物车中的商品数量。
- 添加到购物车按钮后,Livesearch的结果绝对没问题。
- 当我第一次将任何产品添加到购物车中时,它将替换头文件中购物车的值&还将"添加到购物车"按钮替换为"已添加"。
- 当我第二次用相同的搜索结果添加产品到购物车时,它将按钮的值更新为"已添加",但它不更新我头部中的购物车值。这就是问题所在。
下面是我的代码:
header。php
<script type="text/javascript">
$(document).ready(function()
{
$(".searchproductbrand").keyup(function()
{
var kw = $(".searchproductbrand").val();
if(kw != '')
{
$.ajax
({
type: "POST",
url: "livesearch.php",
data: "kw="+ kw,
success: function(option)
{
$("#livesearch").show();
$("#livesearch").html(option);
document.getElementById("livesearch").style.border="1px solid #A5ACB2";
}
});
}
else
{
$("#livesearch").html("");
document.getElementById("livesearch").style.border="0px";
}
return false;
});
$(document).delegate('.buynow','click', function(){
var productid = $(this).attr('id');
var quantity = $('#quantity_'+productid).val();
var type= $('#type_'+productid).val();
$.ajax({
type: "POST",
url: "db_addtocart.php",
context: this,
data: {quantity:quantity,
type:type,
productid:productid},
success: function(option){
this.value = 'Added';
$('#carttotal').empty();
$('#carttotal').replaceWith(option);
}
});
return false;
});
});
</script>
<div id='carttotal'><input type='button' class='button' id='cartdetails'
value='<?php echo "Cart (".$proInCart.")"?>' style='padding:7px;
border-radius:15px;'></div>
例如,如果我搜索'he',它会显示14个结果。对于第一个结果,如果我点击"添加到购物车"按钮,它会将该按钮的值更改为"添加"&更新购物车div.但是从第二个请求与相同的搜索结果,它不更新购物车,但它更新按钮的值为'Added'。
为什么?有人能帮忙吗?
您的代码在第一个请求之后不工作的原因是您在函数结束时返回false。您应该将函数更改为如下内容:
<script type="text/javascript">
$(document).ready(function()
{
$(".searchproductbrand").keyup(function(e)
{
var kw = $(".searchproductbrand").val();
if(kw != '')
{
$.ajax
({
type: "POST",
url: "livesearch.php",
data: "kw="+ kw,
success: function(option)
{
$("#livesearch").show();
$("#livesearch").html(option);
document.getElementById("livesearch").style.border="1px solid #A5ACB2";
}
});
}
else
{
$("#livesearch").html("");
document.getElementById("livesearch").style.border="0px";
}
e.preventDefault();
});
$(document).delegate('.buynow','click', function(e){
var productid = $(this).attr('id');
var quantity = $('#quantity_'+productid).val();
var type= $('#type_'+productid).val();
$.ajax({
type: "POST",
url: "db_addtocart.php",
context: this,
data: {quantity:quantity,
type:type,
productid:productid},
success: function(option){
this.value = 'Added';
$('#carttotal').empty();
$('#carttotal').replaceWith(option);
}
});
e.preventDefault();
});
});
</script>