将 2 个值传递给 javascript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1936749/
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
pass 2 values to a javascript function
提问by amit
i am trying to pass 2 values to a javascript xmlHttp request; THE values are being passed to the javascript function. i was successfully passing a single value to the javscript function, but now i need to pass 2 values. the bold values is a string name i want in the javascript.
我正在尝试将 2 个值传递给 javascript xmlHttp 请求;正在将值传递给 javascript 函数。我成功地将单个值传递给了 javscript 函数,但现在我需要传递 2 个值。粗体值是我想要在 javascript 中的字符串名称。
echo "<a href='#' class='thumb'><img class='thumb-img' value = ".$row->aid." onclick='getVote(".$row->aid.", **".$row->atitle."**)' src='images/roadies/th".$row->aid.".jpg' /> </a>";
one is a intand the other is a string
一个是int另一个是字符串
in the javascript, i am not sure how to receive these values. earlier i used to:
在 javascript 中,我不确定如何接收这些值。早些时候我曾经:
function getVote(int)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
value = int;
for a single value. but now since there are 2 values to process, i dont know how to write the function for it; i am currently (unsuccessfully) trying this:
对于单个值。但现在由于有 2 个值要处理,我不知道如何为其编写函数;我目前(未成功)尝试此操作:
function getVote(int, name)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
value = int;
name= char;
Please tell me how to do it?
请告诉我怎么做?
回答by Roland Bouman
If I understand correctly, your question is simply: how do javascript functions receive multiple arguments?
如果我理解正确,您的问题很简单:javascript 函数如何接收多个参数?
This is easy, just separate them by a comma in your function declaration, and pass multiple values, again separated by comma in the function call:
这很简单,只需在函数声明中用逗号分隔它们,并传递多个值,在函数调用中再次用逗号分隔:
function myFunc(one, two) {
alert(one); alert(two);
}
myFunc(1,2);
If you don't know in advance how many arguments to pass/receive, just declare the function without arguments, and use the built-in arguments array inside the function definition:
如果您事先不知道要传递/接收多少个参数,只需声明不带参数的函数,并在函数定义中使用内置参数数组:
function myFunc(){
for (var i=0, numArgs = arguments.length; i<numArgs; i++){
alert(arguments[i]);
}
}
The approach above is nice if you need to pass a list of values that are all equal, but when you have to deal with multiple arguments and some of them are optional, a better approach is to pass an object literal, and add the 'arguments' as properties:
如果您需要传递一个全部相等的值列表,上面的方法是很好的,但是当您必须处理多个参数并且其中一些是可选的时,更好的方法是传递一个对象文字,并添加 'arguments ' 作为属性:
function myFunc(obj){
if (typeof(obj.arg1)!="undefined") {
....
}
if (typeof(obj.arg2)!="undefined") {
....
}
...more handling...
}
myFunc({
arg1: "one"
, arg2: "two"
, ...more properties...
});
回答by Yuliy
You probably want to enclose the title in quotes. Let's say you have a row with aid123and title Hello World. You want to have onclick="getVote(123,'Hello World')"
您可能希望将标题括在引号中。假设您有一行aid123和标题Hello World。你想拥有onclick="getVote(123,'Hello World')"
回答by Lucas Jones
In JavaScript, you don't need to declare variable types in parameter lists. So, your function would be:
在 JavaScript 中,您不需要在参数列表中声明变量类型。所以,你的功能是:
function getVote(myInt, name) {
Make sure, though, when sending your value to the server, use myInt.toString()instead of just myInt, as you did with name.
但是,请确保在将您的值发送到服务器时,使用 ,myInt.toString()而不仅仅是myInt,就像您对name.
回答by Itay Moav -Malimovka
function getVote(int, name)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
alert(int);
alert(name);
}
This variables are all ready defined in your function once you get them as parameters.
My code will alert them to show you they are deined
一旦您将它们作为参数,这些变量都已准备好在您的函数中定义。
我的代码会提醒他们向你展示他们是被定义的

