【发布时间】:2021-12-06 07:39:22
【问题描述】:
所以我想创建一个搜索栏,用 jquery 搜索数据,然后在找到数据后,我通过不刷新页面替换为“div”。然后,当我擦除搜索栏中的所有字符时,它将重置旧的 div。之后,我的 JS 函数将无法再次工作,直到我重新加载页面。
我找不到问题在哪里,我正在使用的记录:
- JQUERY 3.4.1
- js.js 文件
- search.js 文件
我没有将 js.js 与 search.php 混合,因为包含 vanilla js 和 search.js 的 js.js 是 jquery 文件。
搜索栏
<!-- The search menu -->
<form method="GET">
<div class="input-group">
<input type="text" name="search" id="cariProjek" class="form-control search-input" placeholder="Cari projek ...">
</div><br>
</form>
div
<div id="reset">
<div id="search">
<table>
<tr>
<td>
<label for="tempFolderPath" class="form-label"><b>Temp Folder Lokasi</b></label>
</td>
<td>:</td>
<td>
<input type="text" class="form-control" name="tempFolderPath" id="tempFolderPath" placeholder="Ini readonly!!!" readonly></label>
</td>
</tr>
<tr>
<td>
<label for="namaFileBaru" class="form-label"><b>Nama File Baru</b></label>
</td>
<td>:</td>
<td>
<input type="text" class="form-control" name="namaFileBaru" id="namaFileBaru" autocomplete="" required>
</td>
</tr>
<tr>
<td>
<label for="namaFileLama" class="form-label"><b>Nama File yang akan Diubah</b></label>
</td>
<td>:</td>
<td>
<input type="file" class="form-control" id="namaFileLama" multiple onchange="showname()" required>
</td>
</tr>
<tr>
<td>
<label for="tempNama" class="form-label"><b>Temp Nama File</b></label>
</td>
<td>:</td>
<td>
<input type="text" class="form-control" name="tempNama" id="tempNama" readonly>
</td>
</tr>
</table>
<button type="button" name="submit" class="btn btn-primary" id="submitForm">Selesai</button><br><br>
<table>
<tr>
<td>
<label for="folderPath" class="form-label"><b>Folder Lokasi Asal</b></label>
</td>
<td>:</td>
<td>
<input type="text" class="form-control" style="width: 300px;" name="folderPath" id="folderPath" autocomplete="" placeholder="Pertama input ini dulu baru Simpan" require>
</td>
</tr>
</table>
<button type="button" name="submitFolderPath" class="btn btn-success" id="submitFolderPath">Simpan</button>
<button type="button" name="resetFolderPath" class="btn btn-danger" id="resetFolderPath">Reset</button>
</div>
js.js(此代码在 div 重置后将不起作用)
编辑:主要问题在这里。
let submitFolderPath = document.getElementById('submitFolderPath');
let resetFolderPath = document.getElementById('resetFolderPath');
let folderPath = document.getElementById('folderPath');
let tempFolderPath = document.getElementById('tempFolderPath');
submitFolderPath.addEventListener('click', function(){
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200) {
let date = new Date();
date.setDate(date.getDate() + 1);
const expires = "expires=" + date;
document.cookie = "path=" + folderPath.value + "; " + expires + "; path=/";
tempFolderPath.value = folderPath.value;
}
}
xhr.open('GET', 'index.html', true);
xhr.send();
});
resetFolderPath.addEventListener('click', function(){
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200) {
let date = new Date();
date.setDate(date.getDate() - 1);
const expires = "expires=" + date;
let cookie = document.cookie = "path=" + folderPath.value + "; " + expires + "; path=/";
folderPath.value = '';
tempFolderPath.value = folderPath.value;
}
}
xhr.open('GET', 'index.html', true);
xhr.send();
});
search.js (很确定问题就在这里)
编辑:其实问题不在这里
$(document).ready(function(){
$('#cariProjek').on('keyup', function(){
if($('#cariProjek').val() == ''){
$("#reset").load(location.href+" #reset>*","")
}else{
$('#search').load('search.html?keyword=' + encodeURIComponent($('#cariProjek').val()));
}
})
});
search.html(仅测试)
<h1>Test</h1>
【问题讨论】:
-
使用
$("#reset").load(),您将完全替换该元素的内容。即使您之后有具有相同 ID 的元素等 - 这些元素不会是 same 元素,它们是新创建的一次。因此,绑定到 old 元素的事件处理程序现在也消失了。您现在要么需要再次将它们分配给那些新元素 - 要么使用称为事件委托的方法。 -
顺便说一句,这种香草 JS 和 jQuery 的混合至少可以说是丑陋的。是什么阻止您使用 jQuery 方法在您自己的脚本中添加事件处理程序和发出 AJAX 请求...?
-
我明白了,那我该如何重新分配它们?
-
具体来说:
$('#cariProjek').on('keyup',只会绑定到代码运行时存在的 cariProjek。而$(document).on("keyup", "#cariProjek",将在您替换该初始元素后起作用。 -
@freedomn-m 和 CBroe 谢谢,我的问题已经解决了。
标签: javascript html jquery ajax cookies