如何在JavaScript函数中传递逗号单独的字符串?

时间:2014-09-15 07:23:50

标签: javascript asp.net

代码

在.cs页面

string test = "1,2";
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "Script", "OpenCenterWindow(" + test + ");", true);

JavaScript功能

 function OpenCenterWindow(Ids) {
            alert(Ids);
}

在上面我得警报框= 1

我尝试使用Encrypt和Decrypt但是我收到的错误就像Uncaught SyntaxError:Unexpected token) OpenCenterWindow(fBqJaxPUucc =);

但我想1,2  任何解决方案

3 个答案:

答案 0 :(得分:1)

你可以这样做:

string test = "[1,2]";
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "Script", "OpenCenterWindow(" + test + ");", true);

因此,您将在客户端获得一个整数数组:

function OpenCenterWindow(myArray) {
     alert(Ids);
}

答案 1 :(得分:1)

使用如下

string test = "1,2";
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "Script", 
             "OpenCenterWindow('" + test + "');", true);

您需要以单引号'或双引号"传递值

编辑1

当您调用某个函数时,如果必须传递静态值,则始终在javascript中使用'"

答案 2 :(得分:0)

检查这项工作。

  <script>
        ids="1,2";
    function OpenCenterWindow(Ids) {
        alert("herer");
    alert(Ids);
    }

        OpenCenterWindow(ids);