如何使用jquery将url last id追加到搜索框中

问题描述 投票:1回答:2

我试图将URL的最后一个ID附加到DataTable的搜索框中。我的网址如下所示:

在这里,我想得到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();
}); 
javascript jquery ajax codeigniter-2 codeigniter-3
2个回答
1
投票

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

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

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

0
投票
$(document).ready(function () {

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

 l=location.href; // to get current URL
    // or 
    l="http://localhost/eezyclinic/Admin/clinicsubmissionslist/CL00074";

     url_arr = l.split("/") ;
     id = url_arr[url_arr.length-1];
    dt.fnFilter(id);

 });
© www.soinside.com 2019 - 2024. All rights reserved.