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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!