dongxiequ3724 2015-11-17 19:39
浏览 39
已采纳

Codeigniter - 使用JQuery ajax刷新多个内容的多个元素?

what I want to achieve is that multiple divs in my view get fill up with different content using ajax at the same time when a button is clicked, let's say something like this:

view:

<div id="one">
</div>

<div id="two">
</div>

<div id="three">
</div>

div one, two and three should be refreshed with different content each one.

my script is:

$("input[type='button']").click(function(){
        var mail= $("#mail").val();
        var password = $("#password").val();

        $.ajax({
              url: '<?php echo base_url()?>index.php/login/theLogin',
              type: 'POST',
              data: {'mail':mail, 'password':password},
              success: function(data) {
                $('div#one').html(data);
                $('div#two').html(data);
                $('div#three').html(data);
            }
          });
      });

controller:

public function theLogin()
{

  $this->load->view("ajax/newContent");
}

newContent.php:

<div id="ajax-one">
one
</div>

<div id="ajax-two">
two
</div>

<div id="ajax-three">
three
</div>

and the result is obviously the three divs in my view get refreshed with three divs (9 divs in total), but I don't want that, I want that div one gets refreshed only with the content of div ajax-one and so on.

I used something like this in my ajax method but no success:

    success: function(data) {

                    $('#one').html($('#ajax-one' , data).html());
                    $('#two').html($('#ajax-two' , data).html());
                    $('#three').html($('#ajax-three' , data).html());

                }

What possible solution could exists to solve this? thanks.

  • 写回答

2条回答 默认 最新

  • doulin1867 2015-11-17 20:36
    关注

    I would replace the DIVs with the new DIVs. So if you have a parent DIV you can replace the whole thing with the new data.

    Consider this being your initial view:

    <div id="myWrapperDiv">
      <div id="one">
      </div>
    
      <div id="two">
      </div>
    
      <div id="three">
      </div>
    </div>
    

    When your data is returned do this:

    $("#myWrapperDiv").html(data);
    

    UPDATE:

    Since that was not a good option for you I have come up with another possible solution. Personally I would return a JSON array that separated the DIVs, however, I will offer a suggestion to work with your current code. I would split the return into an array and process it from there.

    Let me show you. In the below we split the data on the closing div tags with a limit of 3. Without the limit you will get an empty string at the end of your array. Then we replace the contents of your DIVs with the ajax DIVs. We have to add back the closing div tags since it was removed when we split the data. I hope this helps :).

    var myArray = data.split("</div>",3);
    $("#one").html(myArray[0] + "</div>");
    $("#two").html(myArray[1] + "</div>");
    $("#three").html(myArray[2] + "</div>");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失