javascript 如何在javascript中将字符串值作为参数传递?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8267120/
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-26 02:50:43  来源:igfitidea点击:

How to Pass string value as parameter in javascript?

javascriptparameter-passing

提问by user550884

This is my code:

这是我的代码:

    var Area = "000000002";
    <a class='button orange' id='proceed' onclick='testFunction("+Area+")' href='javascript:;'>Proceed</a>

Now when this value is sent to the next function i.e ....

现在当这个值被发送到下一个函数时,即......

    function testFunction(Area)
    {
    alert(Area)
    }

In this case I get the value 2....what i should be getting is 000000002....

在这种情况下,我得到值 2....我应该得到的是 000000002....

So i guess it is not being sent as a string.

所以我想它不是作为字符串发送的。

I need to know how to get the value 000000002 is the testFunction...

我需要知道如何获得值 000000002 是 testFunction ...

Thanks in advance...

提前致谢...

回答by Martin ?dila

Try this:

试试这个:

var Area = "000000002";
<a class='button orange' id='proceed' onclick='testFunction(&apos;"+Area+"&apos;)' href='javascript:;'>Proceed</a>

回答by Robert Wilson

You really don't want to do this "+Area+";

你真的不想做这个“+Area+”;

function testFunction(Area)
{
    alert(Area.toString())
}
var Area = "000000002";

<a class='button orange' id='proceed' onclick='testFunction(Area)' href='javascript:;'>Proceed</a>

Hope this helps...

希望这可以帮助...

回答by Aks00747

<button class="btn btn-success" type="submit" onclick="myFunction(<%=ppiid+","+price+","+"'"+pname+"'"%>)">add to cart</button> 

 function myFunction(x,y,z)
     {        
        document.getElementById("demo").innerHTML=x+" "+y+" "+z.toString();             
     }

<p id="demo"></p>