Javascript 如何使用jquery-codeigniter更改视图中显示的html内容
我不太确定如何键入这个问题的标题,但无论如何 在我的视图中有这样的代码称为content.phpJavascript 如何使用jquery-codeigniter更改视图中显示的html内容,javascript,php,jquery,html,codeigniter,Javascript,Php,Jquery,Html,Codeigniter,我不太确定如何键入这个问题的标题,但无论如何 在我的视图中有这样的代码称为content.php <div class="fix single_content floatleft"> <h2><img src="<?php echo base_url('assets/images/New.png')?>" alt="" width="24" height="24"></img><a href="<?php echo
<div class="fix single_content floatleft">
<h2><img src="<?php echo base_url('assets/images/New.png')?>" alt="" width="24" height="24"></img><a href="<?php echo base_url('index.php/Main_Controller/loadKeluhan')?>" >New</a> </h2>
<?php include 'tableLoad.php' ?>
<p class="viewall"><a href="#">view more </a> <i class="fa fa-hand-o-right"></i></p>
</div>
问题是每次运行jQuery代码时,它都不显示任何内容,并且浏览器控制台中也没有错误报告。php中的代码运行良好,但jQuery中的代码运行不好
有谁能给我一些启示吗
提前谢谢,对不起,我的英语不好
解决了的
解决方案:
我将js代码更改为Ajax,并使用它从控制器调用函数,就像poonam所说的那样。js文件如下所示:
$(document).ready(function(){
setInterval('autoRefresh_div()', 2000);
});
function autoRefresh_div()
{
$.ajax({
dataType:'text',
type: "POST",
url: BASE_URL+'index.php/Main_Controller/loadKeluhan',
success: function (response){
$("#newTable").html(response);
}
});}
with loadKeluhan是我的主控制器中的一个函数,用于加载tableLoad视图并向其传递新的数组值。您应该使用load()
试试这个:
function autoRefresh_div()
{
$("#newTable").load("tableLoad.php");// a function which will load data from other file after x seconds
}
setInterval('autoRefresh_div()', 2000); // refresh div after 2 secs
并使用以下命令:
<div class="fix single_content floatleft">
<h2><img src="<?php echo base_url('assets/images/New.png')?>" alt="" width="24" height="24"></img><a href="<?php echo base_url('index.php/Main_Controller/loadKeluhan')?>" >New</a> </h2>
<div class="fix"><ul id="newTable">
<?php include 'tableLoad.php' ?>
</ul>
</div>
<p class="viewall"><a href="#">view more </a> <i class="fa fa-hand-o-right"></i></p>
</div>
谢谢你的回答。我以前尝试过这个解决方案,但它给我404错误,因为文件不在同一个目录中。当我尝试使用tableLoad.php的路径加载它时,它给出了403错误禁止。这可能是因为codeigniter的结构,但不知道如何解决这个问题。那么您应该使用
$.ajax()
。它更好、更高效。我还不太了解如何使用ajax。你能给我举个例子如何用ajax解决这个问题吗?我用它作为文件的url路径。url:BASE_url+'application/view/loadTable.php'。但是仍然得到403禁止的错误BASE\u URL
是一个php变量,所以在php标记URL:'application/view/loadTable.php'
中使用它,您需要在控制器中加载视图,然后在ajax中使用此URL。
function autoRefresh_div()
{
$("#newTable").load("tableLoad.php");// a function which will load data from other file after x seconds
}
setInterval('autoRefresh_div()', 2000); // refresh div after 2 secs
<div class="fix single_content floatleft">
<h2><img src="<?php echo base_url('assets/images/New.png')?>" alt="" width="24" height="24"></img><a href="<?php echo base_url('index.php/Main_Controller/loadKeluhan')?>" >New</a> </h2>
<div class="fix"><ul id="newTable">
<?php include 'tableLoad.php' ?>
</ul>
</div>
<p class="viewall"><a href="#">view more </a> <i class="fa fa-hand-o-right"></i></p>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
function autoRefresh_div()
{
$.ajax({
dataType:'text',
type: "POST",
url: 'url_to_your_file or function',
success: function (response){
$("#newTable").html(response);
}
});
}
setInterval('autoRefresh_div()', 2000); // refresh div after 2 secs
</script>