javascript - 将独立的 View 文件放入codeigniter中另一个html中的div中

标签 javascript php jquery html codeigniter

我正在尝试在 <div> 中放置 View 代码点火器中的另一个 View

views/parent_view.php

<div id="my_div" class="tabcontent">
  <h3>Main Div</h3>
  <p>Main Div content.</p>
</div>

我的要求是放置

views/child_view.php

<html>
      <head>
       Child 1
      </head>
      <body>
       I am child one
      </body>
    </html>

controllers/child_view_controller.php

 function loadview(){

      $data['result'] = '';//
      print $this->load->view('child_view',$data,true);
 }

使用ajax调用上述函数

$.ajax({
             url: base_url+'index.php/child_view/loadview',
             type: 'POST',
             dataType: 'html',
             success: function (html) {
                $("#my_div").html=html;


             }
        });

我收到了整个 html 响应,但父 View 中没有任何变化

最佳答案

让我尝试回答这个问题。如果我理解正确的话,您希望将 ajax 请求的响应加载到父 div 中。如果这就是您想要做的,那么这应该会有所帮助。

<!--  this is your parent view -->
<div id="my_div" class="tabcontent">
    <h3>Main Div</h3>
    <p>Main Div content.</p>
</div>

接下来是您的 JavaScript 代码

<!-- this is your ajax -->
$.ajax({
    url: base_url+'index.php/child_view_controller/loadview',
    type: 'POST',
    dataType: 'html',
    success: function (response) {
        $("#my_div").html(response); // this replaces the children of #my_div with the response received from the php
    }
});

然后是加载所需 View 的 php 代码

// and this is your php
function loadview(){
  $data['result'] = '';
  $this->load->view('child_view', $data, true);
}

我相信您的问题是在您的 javascript 代码中找到的,在您用 ajax 成功响应替换 my_div Id 的子项时。所以基本上我只是重做了你所做的,但改变了一些行,检查一下并让我知道它是否对你有帮助。

关于javascript - 将独立的 View 文件放入codeigniter中另一个html中的div中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45720449/

相关文章:

javascript - 使用动态 ID 获取单击的 div 的属性

php - OOP PHP 从类函数输出数据

php - 单击动态创建的复选框时通过 AJAX/PHP 更新 MySQL 中的值?

php - 根据两个或多个变量的存在设置变量的值

javascript - 通过多选隐藏/显示表格行

javascript - 每隔几秒从 html 数组中的值更改类

javascript - 输入文件: How to display the name of a file even after refresh?

javascript - 处理谷歌图表的悬停事件

javascript - Windows 上的 gulp-rsync 不在远程 Linux 服务器上创建目录

javascript - 需要连接单个数组内的多个数组并将每个数组的第一个值设置为键