我的点击不在我使用false搜索时不断返回一个false

发布于 2025-02-02 02:27:56 字数 650 浏览 3 评论 0 原文

我正在使用该数组包含方法来对我的数组中的项目进行故意搜索,但是即使我在那里,它也会在单击按钮时返回false。

var arr = ["steve", "salewa", "Morris"]

var input = document.getElementById("mysearch")
var button = document.getElementById("but1")
but1.addEventListener("click", clicked)

function clicked() {
  var output = arr.includes("input.value")
  console.log(output)
}
<h1>search</h1>
<input type="search" id="mysearch" name="q" placeholder="...search">
<button id="but1">check</button>

I am using the array include method to perform a deliberate search on items in my array but it keeps returning false when I click button,even though they are there.

var arr = ["steve", "salewa", "Morris"]

var input = document.getElementById("mysearch")
var button = document.getElementById("but1")
but1.addEventListener("click", clicked)

function clicked() {
  var output = arr.includes("input.value")
  console.log(output)
}
<h1>search</h1>
<input type="search" id="mysearch" name="q" placeholder="...search">
<button id="but1">check</button>

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

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

发布评论

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

评论(1

琉璃梦幻 2025-02-09 02:27:56

您应该使用 noreflow noreferrer“> 滤除与指定条件匹配的数组值。您需要在数组的每个元素(=每个名称)上使用 incode(),而不是在数组本身上,因为 inclath inder()在数组上仅返回 true false 插入全名时。 include()在数组上工作及其在字符串上的工作方式有很小的区别。您想在名称包含输入字段中输入的内容的情况下返回结果。

另外,您需要使用 input.value 从输入中获取值。如果您用”字符串“ Input.Value” 将使用,而不是您输入的内容。

var arr = ["steve", "salewa", "Morris"]

var input = document.getElementById("mysearch")
var button = document.getElementById("but1")
but1.addEventListener("click", clicked)

function clicked() {
  var output = arr.filter(name => name.includes(input.value))
  console.log(output)
}
<h1>search</h1>
<input type="search" id="mysearch" name="q" placeholder="...search">
<button id="but1">check</button>

You should be using filter() to filter out the array values that match a specified condition. You want to use includes() on each element of the array (= each name), not on the array itself, because includes() on an array only returns true or false when the whole name is inserted. There is a slight difference how includes() works on an array and how it works on a string. You want to return results where the name contains what you entered in the input field.

Also you need to get the value from the input using input.value. If you surround that with " the string "input.value" will be used, not what you entered.

var arr = ["steve", "salewa", "Morris"]

var input = document.getElementById("mysearch")
var button = document.getElementById("but1")
but1.addEventListener("click", clicked)

function clicked() {
  var output = arr.filter(name => name.includes(input.value))
  console.log(output)
}
<h1>search</h1>
<input type="search" id="mysearch" name="q" placeholder="...search">
<button id="but1">check</button>

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