Javascript 在搜索表单中阻止某些单词

Javascript 在搜索表单中阻止某些单词,javascript,php,arrays,wordpress,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=

我一直在网上搜索,但找不到任何我想做的事情。我希望得到你的帮助

尝试一些搜索时出现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、porn之类的词,我希望当他们查找这些东西时,表单不会进行搜索,并留下任何警告,警告无法搜索这些内容,类似于下面的jQuery/Javascript应该可以正常工作

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
}

您希望在
php
js
中实现这一点,因为我没有看到任何
php
代码?有几种方法可以实现这一点。您可以调整此文件中触发搜索时执行的PHP部分,或者将JavaScript事件绑定到搜索栏,该搜索栏在每次按下键时分析输入的文本并显示此警告。请注意,每个人都可以在他们的浏览器中重写JavaScript行为。这是WordPress主题,但我只共享表单,我想知道我如何做到这一点,除了JavaScript不能做其他事情,但如果你能帮助我,使用JavaScript就可以了。我会喜欢@Johannes[Uncaught SyntaxError:missing)after argument list]我不知道您为什么在console中抛出此错误。logEdited,在开始时变量上缺少$但仍然不断向我抛出相同的错误,我在search\u string\u复选框(searchBox,searchButton)后加上a);//在此处启动函数}),错误被删除,但接下来又出现了另一个错误。我将其searchform.js:14保留为未捕获类型错误:searchButton.removeAttribute不是HTMLDocument的search_string_check(searchform.js:14)处的函数。(searchform.js:18)在I(jquery.js:2)处的函数,在Object.fireWith[as resolveWith](jquery.js:2)在Function.ready(jquery.js:2)和HTMLDocument.K(jquery.js:2)中,这是代码[searchButton.removeAttribute(“disabled”);中的第14行