gpt4 book ai didi

javascript - 阻止搜索表单中的某些词

转载 作者:行者123 更新时间:2023-11-30 14:04:46 25 4
gpt4 key购买 nike

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

尝试在搜索时出现 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、色情这样的词 我希望当他们寻找这些东西时,表格不会进行搜索,留下任何警告,警告说这无法搜索

最佳答案

像下面的 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
}

关于javascript - 阻止搜索表单中的某些词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55637510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com