将参数传递给JavaScript函数

将参数传递给JavaScript函数

int i = Convert.ToInt32(Session["sayfaTuru"]);
ClientScript.RegisterStartupScript(this.GetType(), "RefreshOpener", "f2(" + i + ")", true);




function f2(i) {
            if (i == 1) {//CariKartHareketleri
                opener.document.getElementById("HiddenField1").value = "hello world";
                window.opener.location.href = window.opener.location.href; //çağıran sayfayı yeniliyor

            }
            else if (i == 2) {//islemler
                opener.document.getElementById("HiddenField1").value = "hello world";
                window.opener.__doPostBack('GridRefreshPanel.ClientID', '');

            }
            else if (i == 3) {//hizmet listesi
                opener.document.getElementById("HiddenField1").value = "hello world";
                window.opener.__doPostBack('GridRefreshPanel.ClientID', '');

            }

        }


当我调试i中的代码时,它说f2(i)是未定义的。我在做什么错?

编辑:我从这个问题中学到了很多东西,但是我仍然没有答案。我尝试了答案中给出的每个想法,但是i仍然是undefined ...

编辑:由于不确定的原因,我仍然没有解决方案:),但是我接受的答案通常是我的解决方案。

最佳答案

您是否尝试调试服务器端代码以查看是否

int i = Convert.ToInt32(Session["sayfaTuru"]);


首先给您想要的东西吗?



很好地看到上面的内容,我继续进行并创建了自己的测试,并且一切正常。这是我使用的测试:

JS:

<script type="text/javascript">
    function f2(i, hiddenFieldId, updatePanelId) {

        console.log(i + ', "' + hiddenFieldId + '", "' + updatePanelId + '"');
        var hiddenField = window.opener.document.getElementById(hiddenFieldId);

        switch (i) {

            case 1: //CariKartHareketleri
                hiddenField.value = "hello world 1";
                window.opener.location.href = window.opener.location.href; //çağıran sayfayı yeniliyor
                break;

            case 2: //islemler
                hiddenField.value = "hello world 2";
                window.opener.__doPostBack('' + updatePanelId + '', '');
                break;

            case 3: //hizmet listesi
                hiddenField.value = "hello world 3";
                window.opener.__doPostBack('' + updatePanelId + '', '');
                break;

            default:
                alert("error");
                break;

        }
    }
</script>


C#:

Session["sayfaTuru"] = 2; // initialize for testing purposes

int i = Convert.ToInt32(Session["sayfaTuru"]);

string script = string.Format("f2({0}, '{1}', '{2}');",
                              i.ToString(),
                              this.HiddenField1.ClientID,
                              this.GridRefreshPanel.UniqueID);

ClientScript.RegisterStartupScript(this.GetType(), "RefreshOpener", script, true);


console.log输出:

2, "HiddenField1", "GridRefreshPanel"

关于javascript - 将参数传递给JavaScript函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6189406/

10-10 11:42