简体   繁体   English

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

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

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. 我想用JavaScript做类似的事情,但无法弄清楚是否可以将数组传递给类似的函数。 This is my psuedo-function: 这是我的伪函数:

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? 这可以简单地完成吗?

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 within the 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 could use &quot; 最简单的方法是使用单引号,尽管由于属性值的文本是HTML文本(人们往往会忘记),所以您可以使用&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的事实对于数组的内容要牢记……


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 hookEvent function in this other answer to handle the fact that IE8 and earlier don't have addEventListener . 如果需要支持旧版IE,请使用jQuery之类的DOM库,或在另一个答案中使用hookEvent函数之类的东西来处理IE8和更早的版本没有addEventListener的事实。

You have 2 problems. 您有2个问题。 array should be Array , as JS is case-sensitive. array应该是Array ,因为JS区分大小写。 You could also use the short form [1,2,3] . 您也可以使用缩写形式[1,2,3] Then notice that your onclick attribute is wrapped with double quotes. 然后注意,您的onclick属性用双引号引起来。 You need to escape the quotes inside your JS by doing \\" or by simply using ' : 您需要通过执行\\"或简单地使用'对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 JS as TJ Crowder mentioned it. 我还建议避免使用 TJ Crowder提到的内联JS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM