我在JavaScript的简单搜索表中遇到麻烦

发布于 2025-02-06 14:27:23 字数 102 浏览 2 评论 0原文

我知道我坚持的事情很简单。

我的示例在这里:

​一开始什么都没有。当您在搜索表中键入第二或第三个字母时,它们开始出现。那不是很棒吗?

感谢您的帮助。

I know what I'm stuck with is simple.

My example is here: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_filter_list

I want to do the opposite. Nothing appears at first. They start appearing as you type the 2nd or 3rd letter in the search form. Wouldn't that be great?

Thank you for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南风起 2025-02-13 14:27:23

您可以考虑一些方法,因为现在我可以向您展示一个仅稍微编辑您当前代码的解决方案。

为了更好地告诉您我更改了哪些行,我将您的代码移至JSFiddle。

”您描述的功能,一旦输入2个字符或更多字符,它就会开始显示列表。

第一个更改是在HTML部分的第5行中;

<ul id="myUL" style="display: none;">

我们已经做到了,因此您的列表一开始就不可见。

然后在JavaScript部分,第8至12行,我们添加了以下内容;

if (filter.length > 1) {
  ul.style.display = "block";
} else {
  ul.style.display = "none";
}

因此,如果您的过滤器具有超过1个字符,请显示列表,否则隐藏列表。

我希望这有所帮助!如果您愿意,我可以向您展示一些如何更好地构建这样的搜索列表的方法。

There's a couple approaches you can consider, for now I can show you a solution that only edits your current code slightly.

To better tell you what lines I've changed, I've moved your code over to a JSfiddle.

https://jsfiddle.net/ubLve2hx/1/

I've edited your code to fit the functionality you describe, it starts showing the list once you've typed 2 characters or more.

The first change is at line 5 of the HTML section;

<ul id="myUL" style="display: none;">

We've made it so your list isn't visible to begin with.

Then at the javascript section, line 8 to 12 we've added the following;

if (filter.length > 1) {
  ul.style.display = "block";
} else {
  ul.style.display = "none";
}

So if your filter has more than 1 character, show the list, else hide the list.

I hope this has helped! If you want I can show you some approaches on how to better structure search lists like this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文