简体   繁体   English

单击链接后刷新div内容

[英]Refresh div content after click link

Prestashop v1.6 default-bootstrap theme, product-list.tpl file. Prestashop v1.6 default-bootstrap主题,product-list.tpl文件。

I have a button add to cart and div with some smarty code: 我有一个按钮添加到购物车和div与一些聪明的代码:

<a class="ajax_add_to_cart_button" href="....">

<div id="div1">{if $added}Added{else}not added{/if}</div>

Now when I click to link I want to only refresh div content. 现在,当我点击链接时,我想只刷新div内容。

I already try some code finding in stack but my knowledge is still not enought. 我已经在堆栈中尝试了一些代码查找,但我的知识仍未实现。

What I try and it is not work: 我尝试了什么,它不起作用:

$(document).on('click', '.ajax_add_to_cart_button', function() {
    $("#div1").load()       /*also $("#div1").load("#div1")*/
});     

and: 和:

$("#div1").load(location.href + " #div1");

and few more. 还有更多。

EDIT, also this is not work for me: 编辑,这也不适合我:

$(function(){
$(document).on('click', '.ajax_add_to_cart_button', function(e) {
e.preventDefault();
$("#div1").load($(this).attr("href")); /*and this:  $("#div1").load($(this).attr("href #div1"));*/
return false
});
});

EDIT2: When I try this code is half working 编辑2:当我尝试这个代码是一半工作

$(document).ready(function(){
$(".ajax_add_to_cart_button").click(function(){
$("#div1").html("result reloaded successfully");
});
});

why half? 为什么一半? Because text is display but only in first product, and it is no matter which one product I click, and also I try switch html() to load() or refresh() but then is not working. 因为文本只显示在第一个产品中,所以无论我点击哪一个产品,我也尝试将html()切换到load()refresh()但是不起作用。

EDIT3 EDIT3

$(document).ready(function(){
var productid = "{product.id_product}"
$(".ajax_add_to_cart_button").click(function(){
$("#div1" + productid).html("result reloaded successfully");
});
});


<div id="div1{product.id_product}">{if $added}Added{else}not added{/if}</div>

It is display info in all product, all container in product have now different id. 它是所有产品中的显示信息,产品中的所有容器现在都有不同的ID。

try following and check if it works.. 尝试关注并检查它是否有效..

$(function(){
 $(document).on('click', '.ajax_add_to_cart_button', function(e) {
    e.preventDefault();
    $("#one").load($(this).attr("href"));
    return false.
});

});

I noticed you have wrong class in your selector. 我注意到你的选择器中有错误的类。 please check that too. 请检查一下。

Mmm... I see... I guess your problem is that you use the same id for all the products. 嗯......我明白了...我想你的问题是你对所有产品使用相同的ID。 The DOM 'should' have a unique ID for one element, try another way, maybe a simple class as a selector, you couldn't use the same id for all that elements :) DOM'应该'拥有一个元素的唯一ID,尝试另一种方式,也许是一个简单的类作为选择器,你不能对所有元素使用相同的id :)

For example (I'm assuming that your div have mybeautifuldiv class): 例如(我假设你的div有mybeautifuldiv类):

$(document).ready(function(){
    $(".ajax_add_to_cart_button").click(function(){
        $(".mybeautifuldiv").html("result reloaded successfully");
    });
});

If you're trying to do this when there is a list of products (category page etc.) then you need to target a proper div. 如果您在有产品列表(类别页面等)时尝试执行此操作,则需要定位适当的div。 And you might not want to use id but class in your div. 你可能不想在你的div中使用id而是class。

<div class="my-custom-div">{if $added}Added{else}not added{/if}</div>

Is this div inside 'add to cart' anchor? 这个div是'添加到购物车'的锚吗? If it is then you need to target it: 如果是,那么你需要定位它:

$(document).ready(function(){
    $(".ajax_add_to_cart_button").click(function() {
        $(this).find('.my-custom-div').html('Content updated!');
    });
});

If it is not then you can traverse to parent div of product and find your custom div inside it. 如果不是那么你可以遍历产品的父div并在其中找到你的自定义div。 For example in a category page of a default prestashop theme: 例如,在默认prestashop主题的类别页面中:

$(document).ready(function(){
    $(".ajax_add_to_cart_button").click(function() {           
        $(this).closest('.product-container').find('.my-custom-div').html('Content updated!');
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM