页面HTML搜索字符串后出现“not found”告警通知求助
Fixing the "Not Found" Alert for Your Search Feature
Got it, the issue here is that your current checkInput() function uses window.find() but doesn’t actually check whether the search returned any results. Let’s fix that by adding logic to detect when the query isn’t found and trigger your alert.
Updated JavaScript Code
function checkInput() { // Get and clean up the search query var query = document.getElementById('search').value.trim(); // Don't run search if input is empty if (!query) { alert("Silakan masukkan nomor HP yang ingin dicari!"); return; } // Perform the search and capture the result var searchFound = window.find(query); // Show alert if nothing was found if (!searchFound) { alert("Nomor HP tidak ditemukan!"); } // Optional: Reset search state so next search starts from the top // window.find('', false, false); }
Key Improvements Explained:
- Input Trimming: Added
.trim()to remove extra spaces, so accidental whitespace doesn’t trigger a false "not found" alert. - Empty Input Handling: Prevents unnecessary searches when the user clicks "Cari" without typing anything.
- Result Check:
window.find()returns a boolean (trueif the text is found,falseotherwise). We use this value to show your alert when the query isn’t present on the page. - Optional Search Reset: If you want subsequent searches to start from the beginning of the page instead of where the last search left off, uncomment the final line—it clears the browser’s search state.
Quick Tip for Case Insensitivity:
By default, window.find() is case-sensitive. If you want to ignore capitalization in searches, pass true as the second argument:
var searchFound = window.find(query, true); // Case-insensitive search
That should make your search function properly alert users when their query isn’t found on the page.
内容的提问来源于stack exchange,提问作者yagami cell
相关产品推荐
相关产品推荐

