屏蔽搜索表中的某些单词

时间:2019-04-11 16:46:03

标签: javascript php arrays wordpress

我一直在搜索互联网,但找不到任何可以做的事情。我希望得到您的帮助

尝试一些在搜索时出现404错误的操作,但这对我不起作用,因为我不希望搜索完成

<div class="header-search">
    <form method="get" id="searchform" action="/?s=">
        <input class="input-group-field" value="Search..." name="s" id="s" onfocus="if (this.value == 'Search...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Search...';}" type="text">

        <input class="fa fa-search" type="submit" id="searchsubmit" value="">
    </form>
</div>

我想阻止xxx,xnxx,色情之类的词,我希望当他们寻找这些东西时,表单不进行搜索,并留下任何警告,提示无法搜索

1 个答案:

答案 0 :(得分:2)

类似于下面的jQuery / Java脚本应该可以正常工作。

jQuery(document).ready(function( $ ) {
    var searchBox = $("#searchform #s").val(); //Get value of search field
    var searchButton = $("#searchform #searchsubmit"); //Get search button element

    var search_string_check = function(searchBox,searchButton) {

        var blockedWords = "xxx,porn,sex"; //define blocked words here
        blockedWords = blockedWords.split(','); //turn string into array

        if(blockedWords.includes(searchBox)){ //check if the searched value is in the blocked words list
            searchButton.attr("disabled", ""); //if yes disable the button
            alert("Your search contains a blocked word: " + searchBox + ". To continue your search please remove the blocked word."); //alert saying they cannot search for their entered blocked word
        } else {
            searchButton.removeAttr("disabled"); //if no remove disabled button
        }
    }

    search_string_check(searchBox,searchButton); //fire the function

    $('#searchform #s').change(function() { //on change of the search box value do something
        searchBox = $("#searchform #s").val(); //Get value of search field
      search_string_check(searchBox,searchButton); //fire the function
    });
});

您可能更愿意在向上或向下键上进行操作,而不仅仅是更改值,这取决于您,如果是的话,将代码的最后部分替换为:

$('#searchform #s').keyup(function() { //on change of the search box value do something
    search_string_check(searchBox,searchButton); //fire the function
}