gpt4 book ai didi

javascript - 如果我的数组包含使用 jQuery.inArray() 传递的值,如何显示警报?

转载 作者:行者123 更新时间:2023-11-30 12:04:00 25 4
gpt4 key购买 nike

我将文本框的值存储在一个数组中。然后,我通过用逗号分隔的值将值存储在一个数组中。

当我在 ExistsInArray 函数中传递一个值时,我没有收到警报。

<input value="1,2,3,4" id="someid">

JavaScript

var txtboxVal=$("#someid").val();
var myarray=txtboxVal.split(",")

function ExistsInArray(id) {
if (jQuery.inArray(id, myarray) !== -1) {
alert('id');
}
ExistsInArray(3);
}

Fiddle Demo

最佳答案

您有几个问题:

  • id 变量传递给 alert(),而不是字符串,例如 alert(id)
  • ExistsInArray() 调用移到外部 - 您正在创建一个无限循环
  • 您向函数提供了一个 int 值,但该数组包含字符串。

试试这个:

var txtboxVal = $("#someid").val();
var myarray = txtboxVal.split(",")

function ExistsInArray(id) {
if ($.inArray(id, myarray) !== -1) {
alert(id);
}
}

ExistsInArray('3'); // note the quotes here

Working example

关于javascript - 如果我的数组包含使用 jQuery.inArray() 传递的值,如何显示警报?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35794170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com