从javascript调用控制器方法 [英] Calling controller method from javascript
问题描述
我需要从Javascript中调用名为 test()
的控制器方法。脚本代码如下:
I need to call a method of controller named test()
from a Javascript. Script code is given below:
function changeColor(id, color)
{
element = document.getElementById(id);
qnid = document.form1.pickupFrom.qnid;
//window.location = "<?= site_url('controller/test') ?>"
}
有可能吗?以及如何传递参数?
Is it possible? And how to pass the parameters?
推荐答案
这取决于您需要传递的参数。它们是PHP参数吗?你做得很好,或者更好地看@Sudhir回答。
It depends on what parameters youn need to pass. Are they PHP parameters? The you're doing just fine, or better see @Sudhir answer.
如果你的params来自javascript你就不能使用 site_url( )
函数(因为当它详细阐述并吐出url时,javascript尚未执行)。以这种方式构建URL会容易得多:
If your params come from javascript you shoulnd't use the site_url()
function (since when it elaborates and spit out the url, javascript is yet to be executed). It would be much easier to build a URL this way:
function changeColor(id, color) {
element = document.getElementById(id);
qnid = document.form1.pickupFrom.qnid;
window.location = "<?php echo base_url();?>index.php/controller/test/"+element+"/"+qnid;
}
我假设这两个是您要传递的参数。
I'm assuming those two are the parameters you want to pass.
这篇关于从javascript调用控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!