Javascript 当另一个搜索是action';D
我有一个搜索框,显示一个列表项,并在用户单击按钮后清除搜索字段…但是如果用户要再次搜索,我如何删除搜索项?因此,页面只显示新的搜索结果,而不显示以前的搜索结果Javascript 当另一个搜索是action';D,javascript,jquery,Javascript,Jquery,我有一个搜索框,显示一个列表项,并在用户单击按钮后清除搜索字段…但是如果用户要再次搜索,我如何删除搜索项?因此,页面只显示新的搜索结果,而不显示以前的搜索结果 var noNameFound = true; var searchedStudent = []; // Stores names in an array where names can be pulled and shown when button is clicked var studentArr = []; $('.studen
var noNameFound = true;
var searchedStudent = [];
// Stores names in an array where names can be pulled and shown when button is clicked
var studentArr = [];
$('.student-list').children().each(function(){
studentArr.push(this);
});
// this will search through the array to locate the student.
$('.userSearch').on('click', function(){
//stores users search into a variable
var searchRequest = $('#search-input').val();
//search through array here...
for (var i = 0; i < studentArr.length; i++) {
var studentstr = studentArr[i].innerText;
if (studentstr.indexOf(searchRequest) !== -1) {
console.log('YAY!! the name:: ' + searchRequest + ' ::is in our list');
$(".student-list li").css("display","none"); // removes all students from the list.
searchedStudent.push(studentArr[i]);
$(searchedStudent).show(); // displayes the searched student.
noNameFound = false;
}
}
// removes stundent-item if no user can be found, and asks user to click buttont to refresh page.
if (noNameFound) {
var errorMessage = $('<p class="errMessage">Sorry but we were unable to locate the name you were looking for, <br> please click <button class="refresh">here</button> to refresh the page and try again.</p>');
$(".student-list").prepend(errorMessage);
$('.student-item').css('display', 'none');
$('.refresh').on('click', function(){
location.reload();
});
}
if(noNameFound){
//stops users searching an empty search field.
$('.userSearch').prop('disabled',true);
$('#search-input').keyup(function(){
$('.userSearch').prop('disabled', this.value === ""? true : false);
});
}
});
var noNameFound=true;
var searchedStudent=[];
//将名称存储在一个数组中,在该数组中单击按钮时可以提取和显示名称
var studentArr=[];
$('.student list').children().each(function()){
学生推(这个);
});
//这将在数组中搜索以定位学生。
$('.userSearch')。在('click',function()上{
//存储用户搜索到的变量
var searchRequest=$(“#搜索输入”).val();
//在这里搜索数组。。。
对于(变量i=0;i很抱歉,我们无法找到您要查找的名称,
请单击此处刷新页面,然后重试。”;
$(“.student list”).prepend(错误消息);
$('.student item').css('display','none');
$('.refresh')。在('click',function()上{
location.reload();
});
}
如果(未找到){
//停止用户搜索空搜索字段。
$('.userSearch').prop('disabled',true);
$(“#搜索输入”).keyup(函数(){
$('.userSearch').prop('disabled',this.value==“”?true:false);
});
}
});
这就是我到目前为止所做的您需要在单击事件处理程序中的循环之前重置
searchedStudent
数组
// rest of the code
// this will search through the array to locate the student.
$('.userSearch').on('click', function(){
//stores users search into a variable
var searchRequest = $('#search-input').val();
searchedStudent = []; // <<<<<<<<<<<<<<< HERE
//search through array here...
for (var i = 0; i < studentArr.length; i++) {
var studentstr = studentArr[i].innerText;
// rest of the code
//代码的其余部分
//这将在数组中搜索以定位学生。
$('.userSearch')。在('click',function()上{
//存储用户搜索到的变量
var searchRequest=$(“#搜索输入”).val();
searchedStudent=[];//我需要更多的代码来查看,但我猜您不会在单击后删除数组,请尝试在$('.userSearch')之后添加。在('click',function()…searchedStudent=[];这是我的提琴,你需要在刚刚更新的alsosorry中添加html代码:这太简单了,我只需要在函数中声明数组。我能问一下为什么吗?我明白了。但是感谢你帮我解决了这个问题……非常感谢