javascript - 如何通过点击 JavaScript 输入文本字段内的十字按钮来刷新我的实时搜索?
问题描述
我正在进行实时搜索。我使用 ajax 进行了实时搜索,无论我在其中写什么字母,它都会显示与该字母或单词的匹配结果。但是当我单击搜索字段内的十字按钮时会出现问题,它会清除输入字段内的单词,但它不会刷新我的搜索,它不会在搜索之前显示数据,而是显示上一次搜索的结果。
我希望我的实时搜索交叉按钮功能就像这样 - https://codepen.io/gastonbesada/pen/eqvJK 但这是有角度的。我想要 JavaScript 中的代码。
这是我的代码 -
//让这个页面名称为index.php
<div class="search">
<i class="fa fa-search" ></i>
<input type="text" id="searchterm" name="searchterm" placeholder="Search" >
</div>
<table id="result"></table>
//这是index.php的脚本
<script>
$(document).ready(function(){
load_data();
function load_data(query)
{
$.ajax({
url:"search.php",
method:"post",
data:{query:query},
success:function(data)
{
$('#result').html(data);
}
});
}
$('#searchterm').keyup(function(){
var search = $(this).val();
if(search != '')
{
load_data(search);
}
else
{
load_data();
}
});
});
</script>
/* 搜索输入框内十字按钮的脚本*/
<script>
(function ($, undefined) {
$.fn.clearable = function () {
var $this = this;
$this.wrap('<div class="clear-holder" />');
var helper = $('<span title="Press Backspace" class="clear-helper">×</span>');
$this.parent().append(helper);
$this.parent().on('keyup', function() {
if($this.val()) {
helper.css('display', 'inline-block');
} else helper.hide();
});
helper.click(function(){
$this.val("");
helper.hide();
});
};
})(jQuery);
$("#searchterm").clearable();
</script>
解决方案
只需再次调用您在其他部分中所做的空值的 ajax。
helper.click(function(){
$this.val("");
$this.trigger("keyup");
helper.hide();
});
推荐阅读
- salt-stack - 是否建议使用自定义执行模块更新grains
- mysql - mysql - 选择某年之后出生的人
- c++ - 我正在尝试从本书中做一个练习:Programming --Principles and Practice Using C++ (Second Edition)
- python - vstack 如何根据 numpy 文档工作
- python - 如何关闭此对话框
- c - 使用外部中断从睡眠模式唤醒 Adafruit Feather 32u4
- javascript - Javascript Async Await 等待两个函数完成
- assembly - 我怎么知道我的代码是什么目标文件格式?
- vhdl - 1039 VHDL Interface Declaration error in ...:无法读取模式输出的接口对象。将对象模式更改为缓冲区
- xml - XSD 错误:没有可用于验证根的匹配全局声明