我正在尝试在 <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/