学生搜索项目jquery/javascript
Student search project jquery / javascript
我正在做一个项目,该项目要求我在一个包含学生姓名和电子邮件地址列表的页面上添加一个搜索栏,并且在提交表格后,只显示在搜索栏中键入了电子邮件地址或学生姓名字符的学生。
到目前为止,我已经附加了搜索栏,并启动了一个函数,该函数侦听按钮上的单击事件,并将输入中的值添加到变量中。
我想我可以做一个if语句,检查列表项是否包含我创建的输入变量,然后添加一个类,然后我可以稍后再做一个if语句,根据它包含的类来隐藏或显示。
$(document).ready(function() {
//set number of students to show on each page
var inputValue;
var showStudents = 10;
//append search bar to the page
$(".page-header").append('<div class="student-search"><input placeholder="Search for students..."><button>Search</button></div>');
//add pagination to the page
$(".page").append('<div class="pagination"><ul></ul></div>');
//make page display 10 students at a time
//make search input only show students that contain that letter.
//use contains to add an ID?/class? to the student list item that matches the value in the input field.
//display the students that have said ID/class and hide the others.
$("button").click(function() {
//store the value of the input search into a variable
inputValue = $("input").val();
console.log(inputValue);
//filter the list items by the input value and add CSS
if ('.student-details h3:contains(inputValue)') {
$('.student-details h3').addClass('showstudent')
}
});
});
显然,我的问题是,一旦我写了这篇文章,它就会将类添加到所有项目中。student详细说明h3项目,而不仅仅是包含inputValue的项目,我不能使用"this",因为它只是将类添加在按钮中。这是最好的编码方式吗?我是否应该将我的学生列表转换为一个数组,并让输入值搜索该数组,然后将结果返回到一个新数组中?我有点迷路了!谢谢你的帮助。
您可以尝试这种方法:
$('.student-details h3').each(function() {
if ($(this).text() == inputValue) {
$(this).addClass('showstudent');
}
}
相关文章:
- 动态地改变“”的URL;添加新项目”;链接使用javascript/jquery
- Javascript(jQuery)给了我奇怪的结果
- 为什么不是't窗口.恢复正常工作吗?(javascript/jquery)
- Javascript/jQuery中的并行Ajax调用
- 我可以在Javascript/jQuery中使用一个变量作为键吗
- Javascript/Jquery/PHP加载页面-如何
- Javascript/Jquery Blob not showing Chrome PDF
- 如何使用javascript/jquery获取iframe的URL的锚点属性
- OnClick/Onhover Javascript/jquery
- 如何从javascript/jquery中的复选框中获取布尔值
- javascript/jquery将utc转换为短格式的本地时间
- 在不设置协议的情况下,使用javascript/jquery更改iframe-src
- 如何向这个javascript/jquery函数添加参数
- 用JavaScript/jQuery点击图片,选中多个方框
- 使用Javascript/JQuery获取JSON GET数据
- 从数组中删除元素的最佳方法是:javascript/jquery
- Javascript/jQuery压缩一个图像正确的评论帖子,并使用Ajax Laravel 5.2保存到控制器中
- 用DRY方式Javascript/JQuery动态替换HTML
- 类似Javascript JQuery的库
- 如何在 Javascript/jQuery 中获取一些随机下拉列表的选定值