javascript - 如何将url中的变量php获取到javascript

标签 javascript php codeigniter

我有一个 JavaScript 代码

$(document).ready(function() {

            //datatables
            table_pertanyaan = $('#table_pertanyaan').DataTable({ 

                "processing": true, //Feature control the processing indicator.
                "serverSide": true, //Feature control DataTables' server-side processing mode.
                "order": [], //Initial no order.

                // Load data for the table's content from an Ajax source
                "ajax": {
                    "url": "<?php echo site_url('pertanyaan/ajax_list/')?>/" + id,
                    "type": "POST"
                },

                //Set column definition initialisation properties.
                "columnDefs": [
                { 
                    "targets": [ -1 ], //last column
                    "orderable": false, //set not orderable
                },
                ],

            });

我在 URL 中有一个参数 localhost/ci/edit/87 87 是 id。

如何从 URL 获取变量以使用 Javascript 代码?

最佳答案

如果您使用 CodeIgniter,有更好的方法来做到这一点。

这是针对您的问题的特定答案,需要 JavaScript 答案。

假设网址始终在 id 之前具有 edit

var url_parts = window.location.pathname.split('/');
var id =  url_parts[url_parts.indexOf("edit")+1];

如果您想要 CodeIgniter 解决方案,我建议您搜索它,因为它已经回答或评论,我会告诉您。

关于javascript - 如何将url中的变量php获取到javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46999061/

相关文章:

model-view-controller - Codeigniter Controller 间通信

javascript - Reactjs 在特定时间后连续发送 Promise

JavaScript 测试日期(字符串格式)是否超过 30 天前

php - 在 codeigniter 中加密时避免使用特定字符?

php - Zend - 从 Controller 访问布局属性

php - 在 SQL 中搜索字符串

php - CodeIgniter 路径搞乱了 CSS 背景图像指令

javascript - 使用图像而不是实际的 radio 输入有什么好处

javascript - 依赖自动完成脚本

php - PHP 中的字符集检测