javascript - 如何使用 jquery 将 url 最后一个 id 添加到搜索框中

标签 javascript jquery ajax codeigniter-2 codeigniter-3

我正在尝试将 URL 的最后一个 id 附加到数据表的搜索框中。我的网址如下所示:

在这里,我想要获取 CL00074 值并将其附加到搜索框中,如下所示:

[![在此处输入图像描述][1]][1]

<style>
    thead {
        .fix-search & {
            position: fixed;
            top: 10px;
            input {
                width: 250px;
            }
        }
    }
</style>

<script>  
    //this is for search 
    $(document).ready(function () {
        $('#dataTables-example').DataTable({
            responsive: true,
            "order": [[0, "desc"]],
            "columnDefs": [{
                "targets": 'no-sort',
                "orderable": false
            });
        });
</script>
<script>
    var url = window.location.href;
    var id = url.substring(url.lastIndexOf('/') + 1);
    // alert(id);

    $(document).ready(function () {
        $('#dataTables-example').append(id);
    });
</script>

如果你们不明白我在问什么,请告诉我。非常感谢任何帮助

->谢谢你们@Rory McCrossan 和@Jameel Ahmed Ansari 的回复。我已经找到解决方案了。

    var id = url.substring(url.lastIndexOf('/') + 1);
// alert(id);
$(document).ready(function () {
    $('#dataTables-example_filter input').val(id);
    $('#dataTables-example_filter input').keyup();
}); 

最佳答案

首先,要检索 URL 的所需部分,将其拆分为数组并检索最终元素会更容易。然后,您可以使用 dataTable 的 fnFilter() 方法以编程方式搜索该值:

var dt = $('#dataTables-example').dataTable({
    // setup here
});

var id = window.location.href.split('/').pop();
dt.fnFilter(id); // perform the search.

关于javascript - 如何使用 jquery 将 url 最后一个 id 添加到搜索框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41137676/

相关文章:

javascript - 在 AJAX 调用时将 jQuery 对象转换为字符串

javascript - 谷歌图表在第一次请求时不显示,但仅在第二次请求时显示

javascript - Google Drive 始终通过 API 导出空白 pdf

jquery - 获取溢出的div的高度:auto;

javascript - 获取悬停时选择框的值或在同一事件中更改

javascript - 更新 Angular2 中的 View

ajax - Wordpress 管理员/ajax 未检测到用户已登录

javascript - @syncfusion/ej2-ng-grids 中 GridComponent 的依赖关系解决

javascript - 在以下场景中如何在 $.ajax() 函数的 data 属性中发送参数?

php - Codeigniter中通过ajax将数据传递给 Controller