【问题标题】:Datatables: Search for a specific string when a specific button is clicked数据表:单击特定按钮时搜索特定字符串
【发布时间】:2016-12-09 07:04:36
【问题描述】:

我的想法是单击 button1 以在 textare 中获取输入并在 Datatables 中进行搜索。

我需要帮助,希望理解。

我的代码是这样的,但我没有得到结果。我有其他方法,但我不想刷新页面:

<script src="js/ie/jquery.placeholder.js"></script>
<script>
  $(function() {
    $('input, textarea').placeholder();
  });
</script>

然后这样称呼它:

<p class="btn btn-primary" id="myInput" onclick="updateInput('button1')">button1</p>

【问题讨论】:

  • 你的数据表实现在哪里?
  • 在寻求解决方案之前,您应该知道如何先提出问题。如果您的问题不够清楚,我们真的无法帮助您
  • 所以您是说,搜索框中的文本是什么...当您单击按钮时,它将获取搜索框的 .val() 并执行数据表查询,对吗?
  • @Birdy YES,例如:当您单击 button1 时,它将在该搜索框中搜索:button1。和手动输入一样。

标签: javascript php jquery datatables


【解决方案1】:

由于您在问题中提供的信息几乎为零,除了您想通过单击按钮搜索数据表这一事实之外,这应该适合您。

var oTable = $("#myTable").DataTable();

$("button1").on('click', function(event){
  var searchText = "button1"; //Specific text that you want to search
  oTable.search(searchText).draw();
}

这是fiddle

【讨论】:

  • 感谢您的回答,但它不起作用,我试过这个:&lt;button class="btn m-b-xs w-xs btn-info" id="button1"&gt;button1&lt;/button&gt; &lt;script&gt; var oTable = $("#example").DataTable(); $("button1").on('click', function(event){ var searchText = $(this).val(); oTable.search(searchText).draw(); } &lt;/script&gt;
  • 对不起,按钮没有val。我会更新我的答案。
  • 我找到了方法,但下拉过滤器无法正常工作。 #函数$("#acctypqee").on('click', function () { table.column(0) .search(this.id) .draw(); });
【解决方案2】:

如果我认为您正在尝试搜索文本区域中的任何值是正确的,那么我下面的示例将使您继续前进..

警告...人们不是读心者,您(寻求帮助的人)提供的更多帮助可以让我们更好地了解您想要实现的目标。

如果您使用 jQuery,因为您已在 OP 中标记了它。

点击按钮后,这里 jQuery 将获取输入字段的 .val()。

$("#YourButtonIdHere").click(function(){

    // This will get the value of the search input text box 
    var inputText = $("#myInput").val();

    // Now you can do what you wish, Perform an ajax query?
    // More help from you on what you want to do with the value 
    // from the text box once the button has been clicked 
    // will give me a better chance of helping you!
});

【讨论】:

  • 谢谢,但我做不到。我想要的只是单击 button1 以在数据表中进行搜索。就像在搜索框中手动输入:button1。对不起,希望你能理解。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-18
  • 2013-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多