如何在codeigniter中的ajax之后仅加载指定的div

时间:2016-06-10 12:26:10

标签: javascript php jquery ajax codeigniter

根据我的点击,我有一个特定的类别列表,查询已被更改,我还必须根据我的点击更改div

我尝试了检索正确结果的内容,但它在div中显示整个页面..

登陆控制器

public function index()
    {   
        .
        .
        .

        if(isset($_POST['cat']))
        {
            $this->load->model('product_model'); 
            $condition=" WHERE p.status='Publish' AND u.group='Seller' AND u.status='Active' AND p.category_id IN (4) OR p.status='Publish' AND p.user_id=0 AND a.pricing IS NOT NULL AND p.category_id IN (4) GROUP BY p.id ORDER BY p.created DESC";
            $products_list_s = $this->product_model->view_product_details($condition);
            #echo "If";print_r($this->data['productDetails']);exit;
        }
        else
        {
            $condition = " WHERE p.status='Publish' AND u.group='Seller' AND u.status='Active' OR p.status='Publish' AND p.user_id=0 AND a.pricing IS NOT NULL GROUP BY p.id ORDER BY p.created DESC";
            $products_list_s = $this->product_model->view_product_details($condition);
        }
        $this->data['productDetails']         = $this->product_model->get_sorted_array($products_list_s,$products_list_u,'created','desc');
        .
        .
        .

        #echo $this->load->view('site/landing/landing',$data, TRUE);
        $this->load->view('site/landing/landing',$this->data);
    }

Jquery的

$('.all_category_li a').on('click', function(e) {
    var cat = 'cat';
    $.ajax({
        type:'post',
        url:'<?php echo base_url();?>site/landing',
        data:{cat:cat},
        dataType:'html',
        success:function(data)
        {
            $('.product_listing').html(data);
        }
    });
});

查看

<div>
<ul class="product_listing" id="product_listing">
.
.
.
</ul>
</div>

我应该怎么做..

有人可以帮帮我..

2 个答案:

答案 0 :(得分:1)

在jQuery中,您可以删除结果以仅显示元素内的结果。例如:

success:function(data)
{
    // Search Element (replace the element or ID with your element)
    var startPos = data.indexOf("<div id='divId'>");

    // Div element is found?
    if(startPos != -1)
    {
        // Add tag length to position
        startPos += 16;

        // Search Div closing tag starting from start position
        var endPos = data.indexOf("</div>", startPos);

        // Add data inside the Div element to the product listing Div
        $('.product-listing').html(data.substr(startPos, endPos - startPos));
    }
}

答案 1 :(得分:1)

也许你需要这样的东西

$( "#result" ).load( "ajax/test.html #container" );

当此方法执行时,它会检索ajax / test.html的内容,但随后jQuery会解析返回的文档以查找ID为container的元素。此元素及其内容将插入到具有结果ID的元素中,并且将丢弃检索到的文档的其余部分。

http://api.jquery.com/load/