简体   繁体   English

在javascript函数中传递参数

[英]passing an argument in javascript function

i have a button with id Button1 on page load function im trying to call javascript function like this 我在页面加载功能上有一个ID为Button1的按钮,我试图像这样调用javascript函数

int l = files.Length;
Button1.Attributes.Add("onclick", " alertMe(l);");

where files.length is some integer value,now im trying to pass this value in alertMe function can anyone tell me is it a write way to pass the value if yes how can i retrieve it in alertMe function 其中files.length是某个整数值,现在我试图在alertMe函数中传递此值,谁能告诉我这是一种传递值的写方法(如果是)我如何在alertMe函数中检索它

int l = files.Length;
Button1.Attributes.Add("onclick", " alertMe(" + l + ");");
function alertMe(length)
{
    alert("you passed a length of: " + length);
}

In your sample, the value passed to the javascript function is always 1. Also, you might want to use the Button.OnClientClick property instead, as this ensures that ASP.NET's own button handling code is left intact. 在您的示例中,传递给javascript函数的值始终为1。此外,您可能希望改用Button.OnClientClick属性,因为这可以确保ASP.NET自己的按钮处理代码保持不变。 Your C# code should probably look something like this: 您的C#代码应该看起来像这样:

int fileCount = files.Length;
Button1.OnClientClick = "alertMe(" + fileCount + ");"

In the javascript, make sure you declare the formal parameter in the function signature: 在javascript中,确保您在函数签名中声明了形式参数:

function alertMe(fileCount)
{
    alert(fileCount);
}

try: 尝试:

int l = files.Length;
Button1.Attributes.Add("onclick", " alertMe(" + l.ToString() + ");");

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

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