0

我正在使用下面的代码从 PHP 页面中的数据库查询加载结果:

<a href="results.php?item=itemId">click me</a>

$('.item > a').click(function(){
    var url = $(this).attr('href');
    $('.item-popup').fadeIn('slow');
    $('.item-content').load(url);
    return false; 
});

现在一切正常,但下一点功能是个问题。在results.phpajax 加载到 .item-content 的内部,我有另一个链接应该更新和增加该链接的点击次数,也无需刷新。功能性的 PHP 位都可以正常工作。我唯一的问题是事物的 jQuery/AJAX 方面。

也许我走错了路,但我真正想做的是有一个带有容器的页面,该容器从 PHP 页面加载数据库查询的结果,而且在那个容器中,我有一个链接/我希望能够在不刷新的情况下保存和更新所有点击次数的按钮。

编辑 我想我需要回答的最重要的问题是:当 index.php 上的 ajax 将 results.php 的内容加载到 index.php 的容器中时,浏览器是否将新加载的 ajax 内容视为父页面的一部分(索引.php) 还是仍将其视为像 iFrame 一样加载到容器中的不同页面?

4

2 回答 2

1

Dont know for sure if you want this, When returning the data in the load function you will have to add a link like this in the resultant HTML which will be clickable:

   <a href="javascript:void(0);" class"clickable" data-id="<?=$id?>" data-counter="0"></a>

Now in javascript you need to catch the click event of the link like this:

   <script type="text/javascript">
    $(function(){
       $(".item-content").on("click", ".clickable", function(){
            var counter = $(this).data('counter');
            var id      = $(this).data('id');
            $.ajax({
               url      : //your url here,
               data     : {'id' : id, 'counter' : counter },
               type     : 'POST',
               success  : function(resp){
                    //update the counter of the current link
                    $(this).data('counter', parseInt( $(this).data('counter') )+1 );
                    //whatever here on successfull calling of ajax request
               },
               error    : function(resp){

               }
            });
       });     
    });
    </script>
于 2013-06-14T13:24:43.353 回答
1

如果说例如它是单击事件,那么您需要编写

$('input element').on('click',function() {
     // write code over here
})
于 2013-06-14T12:14:18.480 回答