【发布时间】:2018-02-16 07:42:01
【问题描述】:
我正在为 Jquery 中的学生列表开发搜索功能,虽然我可以将正确的结果打印到控制台,但结果并未出现在页面上。匹配时,我在控制台中收到以下错误。我只是不确定我在选择器上声明 listStudents 变量的方式是否有问题?不可能在这样的选择器数组上运行每个函数吗?我难住了。
Uncaught TypeError: listName.each is not a function
at showPage (main.js:10)
at searchList (main.js:92)
at HTMLButtonElement.<anonymous> (main.js:54)
下面是我的JS文件
var listStudents = $(".student-list").children();
var numStudents = listStudents.length;;
function showPage(pageNum, listName) {
// first hide all students on the page
$(".student-list li").hide();
pageNum = parseInt(pageNum);
// Then loop through all students in our student list argument
listName.each(function(index){
// if student should be on this page number
if ((index >= ((pageNum*10)-10)) && (index <= (pageNum*10))) {
// show the student
$(this).show();
}
});
}
function getNumPages(numStudents){
numPages = Math.ceil(numStudents/10);
return numPages;
}
function appendPageLinks(numStudents) {
// determine how many pages for this student list
pages = getNumPages(numStudents);
// create a page link section
var nav = "<div class='pagination'><ul>"
for (i=1; i<pages+1; i+=1){
nav += ("<li>" + "<a href='#' id=" + i + ">" + i + "</a>" + "</li>");
};
nav += ("</ul></div>");
$(".student-list").after(nav);
// define what happens when you click a link
var active = $('.pagination a').click(function(){
// Use the showPage function to display the page for the link clicked
var id = $(this).attr('id');
showPage(id,listStudents);
// mark that link as “active”
active.removeClass('active');
$(this).addClass("active");
});
}
function appendSearchBox(){
var search = "<div class='student-search'><input id='search' placeholder='Search for students...'><button>Search</button></div>"
$(".students").after(search);
// Add click event handler
$("button").click(function() {
searchList();
});
}
function searchList() {
var matched = []
// Obtain the value of the search input
input = $("#search").val();
// remove the previous page link section
$('.pagination').hide();
// Loop over the student list, and for each student…
listStudents.each(function(){
// ...obtain the student’s name…
var name = $(this).find("h3").text();
// ...and the student’s email…
var email = $(this).find(".email").text();
// ...if the search value is found inside either email or name…
if (name.includes(input) || email.includes(input)) {
// ...add this student to list of “matched” student
matched.push($(this));
$(".student-list").hide();
console.log(email);
console.log(name);
}
});
// If there’s no “matched” students…
if (matched.length === 0){
// ...display a “no student’s found” message
$(".student-list li").hide();
var message = ("Sorry, no student's found!");
$("#message").show();
} else if (matched.length <= 10) {
// ...call appendPageLinks with the matched students
$(".student-list li").hide();
showPage(1, matched);
} else {
$(".student-list li").hide();
showPage(1, matched);
appendPageLinks(matched.length);
}
$('#search').value = ''; // Clears search field
}
//Dynamically add search
appendSearchBox();
//Create pagination dynamically
appendPageLinks(numStudents);
//Show first page on load
showPage(1, listStudents);
这是 HTML 中学生列表的示例——我调用的是 Jquery
<div class="page-header cf">
<h2 class="students">Students</h2>
</div>
<ul class="student-list">
<li class="student-item cf">
<div class="student-details">
<img class="avatar" src="https://randomuser.me/api/portraits/thumb/women/67.jpg">
<h3>iboya vat</h3>
<span class="email">iboya.vat@example.com</span>
</div>
<div class="joined-details">
<span class="date">Joined 07/15/15</span>
</div>
</li>
</ul>
</div>
【问题讨论】:
-
$.each(listName, function(index, val){})- 请参阅 $.each 上的 jQuery 文档 -
你用的是什么版本的jquery?
-
请不要发布您的整个代码。 95% 的代码与你的问题完全无关,但你仍然强迫每个人阅读它们。专注于相关的部分并丢弃其余部分。
-
jquery 1.12.4,我了解发布代码的规则,但觉得为了弄清楚发生了什么,可能需要全部查看?
-
@JohnRogerson 最好只发布足够的代码来复制问题,在这种情况下,该问题可能与使用
.each复制错误的数组对象的硬编码版本一样短。也可以在链接 jQuery 的问题中创建 sn-p。有关更多详细信息,请参阅此内容 - How to create a Minimal, Complete, and Verifiable example - 制作一个最小示例来演示您的问题的好处是您在此过程中很多时候自己发现错误:) Win/Win
标签: javascript jquery