[英]How to pass variable from controller to javascript that is not inside a view?
I want to pas this data to a javascript function that is in a javascript file : 我想将此数据粘贴到javascript文件中的javascript函数中:
$data = $this->model_member->fetchMemberData();
The javascript function takes as a parameter the id_member
that's inside the $data
variable.It is called on button click from a view, but if I pass the data to the view I'll have to load it and I don't want that since it is already loaded. javascript函数将
$data
变量中的id_member
作为参数,在视图中单击按钮时会调用它,但是如果我将数据传递给视图,则必须加载它,我不希望这样,因为它已经加载。 How can I pass this data directly to the js function? 如何将这些数据直接传递给js函数?
There are many ways - 有很多方法-
1 - You can use php tags in your js code. 1-您可以在js代码中使用php标签。 So you can access the variable like this -
因此您可以像这样访问变量-
var id_member = "<?php echo $data['id_member'] ?>";
2 - Store that $data in one hidden variable and than access it using any selector. 2-将$ data存储在一个隐藏变量中,然后使用任何选择器访问它。 For ex -
对于前-
<input type="hidden" class="id_member_hidden">
var id = $(".id_member_hidden:hidden").val();
Hope this will help you to solve your problem. 希望这可以帮助您解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.