codeigniter单击按钮调用视图
我的codeigniter视图中有两个按钮:codeigniter单击按钮调用视图,codeigniter,codeigniter-routing,Codeigniter,Codeigniter Routing,我的codeigniter视图中有两个按钮: <div class="btn-main col-md-3 col-md-offset-3"> <button id="simu-mono" type="button" class="btn btn-default">SIMULATION MONO SITE</button> </div> <div class="btn-main col-md-3">
<div class="btn-main col-md-3 col-md-offset-3">
<button id="simu-mono" type="button" class="btn btn-default">SIMULATION MONO SITE</button>
</div>
<div class="btn-main col-md-3">
<button id="simu-multi" type="button" class="btn btn-default">SIMULATION MULTI SITE</button>
</div>
simu_mono.php:
<?php
class simu_mono extends CI_Controller {
public function index()
{
$this->load->view('simu_mono');
echo 'Hello World!';
}
}
?>
谢谢你的帮助
干杯您没有在javascript中进行任何AJAX调用。我假设您使用的是jQuery,因此,您的调用应该类似于:
$("#simu-mono").click(function(){
$.ajax({
url: "http://your-url.com/controller/method",
type: 'post', // <- Or get option, whatever you prefer
dataType: 'json', // <- This is important to manage the answer in the success function
//data: { param1: "value1", param2: "value2"}, <- You could add here any POST params you wanted
success: function(data){
if (data.view) {
$('#here_view').html(data.view); // <- '#here_view' would be the id of the container
}
if (data.error){
console.log(data.error);
}
}
});
});
$(“#simu mono”)。单击(函数(){
$.ajax({
url:“http://your-url.com/controller/method",
键入:“post”,如果您想重定向,请使用以下代码:
$(document).ready(function(){
$("#simu-mono").click(function(){
window.location = base_url + "/simu_mono";
});
$("#simu-multi").click(function(){
window.location = base_url + "/simu_multi";
});
});
请注意,您可能需要base\u url
,请使用此代码段在JavaScript变量中加载base\u url
<script>
base_url = <?= base_url()?>
</script>
通过这种方式,您可以通过以下方式进入页面和控制器:yourserver.ufo/simu mono
和yourserver.ufo/simu multi
您想重定向到url吗?或者使用模式显示视图。嗨,Kyslik,要重定向到urlhi chococroc,您在url中放了什么:“控制器是控制器的名称?请以后忘记控制器中的索引方法(我的意思是,当你访问dev2one.com/simu\u mono时,你会得到相同的结果,因为CodeIgniter框架会首先查找索引函数,除非有\u remap()
存在。)是的,你可以把url:“simu\u mono”
,因为javascript将进入你的根目录,在CI中,如果你没有指定方法,它将使用index()
作为默认方法加载。你没有加载视图吗?你是否在html中添加了一个容器()?你是否检查了收到的视图是否正确?(在Firefox中检查firebug返回的浏览器或在Chrome中开发控制台)嗨,chococroc,我不想返回任何内容,因为这只是从我的实际页面到另一个页面的简单重定向嗨,我尝试了你的解决方案,但是当我连接到我的页面时,我收到了错误消息Objet not found!
$(document).ready(function(){
$("#simu-mono").click(function(){
window.location = base_url + "/simu_mono";
});
$("#simu-multi").click(function(){
window.location = base_url + "/simu_multi";
});
});
<script>
base_url = <?= base_url()?>
</script>
$route['simu-mono'] = "simu_mono";
$route['simu-multi'] = "simu_multi";