简体   繁体   English

是否可以在JavaScript函数中将变量作为参数传递?

[英]Is it possible to pass variable as parameter in javascript function?

I have this function: 我有这个功能:

 protected void mytv_SelectedNodeChanged(object sender, EventArgs e)
 {
    TreeView tv = sender as TreeView;
    var nid = tv.SelectedNode.Value;
    ScriptManager.RegisterStartupScript(this, this.GetType(), "anyname", "show(nid)", true);
}

As you can see, I have a variable nid that gets different value as 1,2,3 according to the treenode clicked. 如您所见,我有一个变量nid,它根据所单击的treenode获得的值为1,2,3。
Can i pass that nid variable value directly as a parameter to the javascript function as show(nid)? 我可以将nid变量值直接作为参数传递给javascript函数show(nid)吗?
If not what do i do? 如果没有,我该怎么办?

My javascript function is as follows: 我的JavaScript函数如下:

function show(nid) 
{

    if (nid == 4) {
        alert('testing');
    }
    else
     {
         alert('what???');
     }
}
//for string value
ScriptManager.RegisterStartupScript(this, this.GetType(), "anyname", string.Format("show('{0}')", nid), true);
//for int value
ScriptManager.RegisterStartupScript(this, this.GetType(), "anyname", string.Format("show({0})", nid), true); 

尝试串联:

ScriptManager.RegisterStartupScript(this, this.GetType(), "anyname", "show(" + nid + ")", true);

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

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