【发布时间】:2019-06-15 12:41:31
【问题描述】:
我正在尝试设置一个搜索栏,该栏搜索通过按名称或电子邮件从 API 获取的循环数据创建的表格,但我找不到我出错的地方。控制台显示未捕获的 ReferenceError: sBar is not defined at window.onload 请记住,我是 JS 的新手。真的很抱歉这很愚蠢,但我已尽力而为,我对无法看到错误感到非常沮丧
这是我的 HTML
<body>
<div>
<label for="finder">Find User:</label>
<input type="search" id="searchInput" name="sInput" placeholder="Search
user">
<button id="sButton">Search</button>
</div>
<table class="table table-responsive">
<thead class="thead-dark">
<tr>
<th scope="col">Id</th>
<th scope="col">Name</th>
<th scope="col">Username</th>
<th scope="col">Email</th>
<th scope="col">Address</th>
<th scope="col">Phone</th>
<th scope="col">Website</th>
<th scope="col">Company</th>
</tr>
</thead>
<tbody name="tTable">
</tbody>
</table>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js">
</script>
<script src="script.js">
</script>
小米JS
window.onload = function(){
let uList = document.querySelector('[name =tTable]');
fetchCall('https://jsonplaceholder.typicode.com/users', getUsers);
sButton.addEventListener('click',
fetchCall('https://jsonplaceholder.typicode.com/users', sBar), false);
function sBar(getObject) {
let sUser = getObject;
let inputBar = document.getElementById("searchInput");
let text = inputBar.textContent;
let textView = text.toUpperCase();
for (let i = 0; i < getObject.length; i++) {
let uObject = sUser[i];
if (textView == uObject.name || textView == uObject.email) {
let new_tTable = document.createElement('tbody');
uList.parentNode.replaceChild(new_tTable, uList)
let row = uList.insertRow();
let idInput = document.createElement('td');
let nameInput = document.createElement('td');
let usernameInput = document.createElement('td');
let emailInput = document.createElement('td');
let cityInput = document.createElement('td');
let phoneInput = document.createElement('td');
let websiteInput = document.createElement('td');
let companyInput = document.createElement('td');
idInput.textContent = uObject.id;
nameInput.textContent = uObject.name;
usernameInput.textContent = uObject.username;
emailInput.textContent = uObject.email;
cityInput.textContent = uObject.address.city;
phoneInput.textContent = uObject.phone;
websiteInput.textContent = uObject.website;
companyInput.textContent = uObject.company.name;
row.appendChild(idInput);
row.appendChild(nameInput);
row.appendChild(usernameInput);
row.appendChild(emailInput);
row.appendChild(cityInput);
row.appendChild(phoneInput);
row.appendChild(websiteInput);
row.appendChild(companyInput);
} else {
alert("User not found");
}
}
}
function fetchCall(url, fn){
fetch(url)
.then(function(response){
return response.json();
})
.then(function(endPoint){
fn(endPoint);
})
.catch(function(error){
console.error(error);
})
}
function getUsers(getObject) {
let user = getObject;
for (let i = 0; i < getObject.length; i++) {
let userObject = user[i];
let row = uList.insertRow();
let idInput = document.createElement('td');
let nameInput = document.createElement('td');
let usernameInput = document.createElement('td');
let emailInput = document.createElement('td');
let cityInput = document.createElement('td');
let phoneInput = document.createElement('td');
let websiteInput = document.createElement('td');
let companyInput = document.createElement('td');
idInput.textContent = userObject.id;
nameInput.textContent = userObject.name;
usernameInput.textContent = userObject.username;
emailInput.textContent = userObject.email;
cityInput.textContent = userObject.address.city;
phoneInput.textContent = userObject.phone;
websiteInput.textContent = userObject.website;
companyInput.textContent = userObject.company.name;
row.appendChild(idInput);
row.appendChild(nameInput);
row.appendChild(usernameInput);
row.appendChild(emailInput);
row.appendChild(cityInput);
row.appendChild(phoneInput);
row.appendChild(websiteInput);
row.appendChild(companyInput);
}
}
}
【问题讨论】:
-
name属性在大部分元素中是没有意义的,只有表单控件元素和窗口元素被名称识别。将id用于其他元素。实际出现什么问题,用帖子里的代码是无法复现的。 -
"sBar is not defined at window.onload" 没有正确描述问题,应该是“sBar is not defined on "Search" button click” ...不要使用内联事件,在
window.onload函数内添加按钮的事件。向sButton添加点击监听器也会失败,fetchCall不会返回任何用作事件处理函数的内容。
标签: javascript ajax for-loop html-table search-box