如何使用 jquery 将 url 最后一个 ID 附加到搜索框中

How to append url last id in into search box using jquery

我正在尝试将 URL 的最后一个 ID 追加到 DataTable 的搜索框中。我的 URL 看起来像这样:

这里我想获取 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.
$(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);

 });