代码之家  ›  专栏  ›  技术社区  ›  radbyx Matt

数据表。如何停止搜索。dt事件从事件处理程序内部触发

  •  1
  • radbyx Matt  · 技术社区  · 9 年前

    我有这个代码,当我检测到用户键入的过滤器包含一些坏字符时 ' 我不希望它到达服务器。

    我有这个简化的代码:

    $table.on('search.dt', function () {
        var value = getValue();
        if (antiHackingHelper.isValid(value)) {
        } else { // Contains charakter like "'" so don't call the server with this bad filter
            // Don't send the request to the server with a filter with bad characters
        }
    });
    
    1 回复  |  直到 4 年前
        1
  •  2
  •   davidkonrad    9 年前

    此时无法阻止执行搜索。 search.dt 在您的 $table.on('search.dt' listener... 它不是可以取消“上游”搜索的链的一部分。

    相反,您可以首先防止在筛选框中输入非法字符:

    var illegalChars = "',.-*)(";
    $('.dataTables_filter input').on('keypress', function(e) {
       if (~illegalChars.indexOf(String.fromCharCode(e.keyCode))) {
         console.log('Illegal char entered, aborting');
         return false;
       }
    })
    

    演示-> http://jsfiddle.net/q39c3c0k/