是否可以将数组从 onClick 传递给 javascript 函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28963562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 09:50:09  来源:igfitidea点击:

Is it possible to pass an array from onClick to javascript function?

javascriptphparraysfunctioninput

提问by Torbj?rn Loke Nornwen

In PHP I can write a function like this:

在 PHP 中,我可以编写这样的函数:

function myFunction(myArray)
{
    foreach(myArray)
    {
        // do stuff with array
    }
}

Then I can call it like this (maybe ugly, but still works):

然后我可以这样称呼它(可能很丑,但仍然有效):

myFunction(array("string1","string2"));

I want to do something similar with JavaScript but can't figure out if it's possible to pass arrays to functions like that. This is my psuedo-function:

我想用 JavaScript 做一些类似的事情,但不知道是否可以将数组传递给这样的函数。这是我的伪功能:

function myFunction(myArray)
{
    for (index = 0; index < myArray.length; ++index)
    {
        // do stuff with array
    }
}

I want to call it from a button using onClick like this:

我想使用 onClick 从按钮调用它,如下所示:

<input type="button" value="myButton" onClick="return myFunction(array("string1","string2"))">

Can this be accomplished in a simle way?

这可以以简单的方式完成吗?

回答by T.J. Crowder

Yes, it's possible. You'd probably want to use array initializer syntax (aka an "array literal"), and you need to mind your quotes:

是的,这是可能的。您可能想要使用数组初始值设定项语法(又名“数组文字”),并且需要注意引号:

<input type="button" value="myButton" onClick="return myFunction(['string1','string2'])">

If your attribute value is quoted with double quotes, you can't use a literal double quote withinthe value. The easiest thing is to use single quotes, although since the text of attribute values is HTML text (something people tend to forget), you coulduse &quot;if you liked. (I wouldn't. :-) ) And the fact that the text is HTML is something to keep in mind for the contents of the array...

如果您的属性值用双引号引用,则不能值中使用文字双引号。最简单的方法是使用单引号,尽管由于属性值的文本是 HTML 文本(人们往往会忘记),如果您愿意,可以使用&quot;。(我不会。:-) )而且文本是 HTML 的事实是要记住数组内容的事情......



It may well not be desirable, though. You might consider using modern event handling techniques to hook up the handler, for instance:

不过,这可能并不理想。您可能会考虑使用现代事件处理技术来连接处理程序,例如:

document.querySelector("input[type=button][value=myButton]").addEventListener("click", function() {
    myFunction(['string1','string2'])
}, false);

If you need to support old IE, use a DOM library like jQuery, or something like the hookEventfunction in this other answerto handle the fact that IE8 and earlier don't have addEventListener.

如果您需要支持旧的 IE,请使用 jQuery 之类的 DOM 库,或其他答案中的类似hookEvent函数来处理 IE8 及更早版本没有.addEventListener

回答by blex

You have 2 problems. arrayshould be Array, as JS is case-sensitive. You could also use the short form [1,2,3]. Then notice that your onclickattribute is wrapped with double quotes. You need to escape the quotes inside your JS by doing \"or by simply using ':

你有2个问题。array应该是Array,因为 JS 区分大小写。您也可以使用简写形式[1,2,3]。然后注意您的onclick属性用双引号括起来。您需要通过执行\"或简单地使用来转义 JS 中的引号'

function myFunction(arr){
  // for demo purposes
  alert( JSON.stringify(arr) );
}
<input type="button" value="myButton" onClick="return myFunction(['string1','string2'])">

I would also suggest avoiding inline JSas T.J. Crowdermentioned it.

我还建议避免使用内联 JS,因为TJ Crowder提到了它。