我有一个 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/