我有这个javascript函数:

<script type="text/javascript">
        function montaDataSubstituicaoPrestador(dt_exclusao) {

            var arrData = dt_exclusao.split('/');
            var exclusaoFormatada = arrData[1] + '-' + arrData[0] + '-' + arrData[2];
            var dias = parseInt(prazoSubPrestador);
            var novaData = new Date(arrData[2], arrData[1] - 1, arrData[0]);

            novaData.setDate(novaData.getDate() + dias);

            hoje = new Date(novaData)
            dia = hoje.getDate()
            mes = hoje.getMonth()
            ano = hoje.getFullYear()
            if (dia < 10)
                dia = "0" + dia

            if ((mes + 1) < 10)
                mes = "0" + (mes + 1);

            if (ano < 2000)
                ano = "19" + ano

            var dt = dia + "/" + (mes) + "/" + ano;

            document.getElementById('lblPrazoSubsAns').innerHTML
                                             = "Prazo de substituição: " + dt;
        }
    </script>


我从后面的代码有这个电话:

ClientScriptManager cs = Page.ClientScript;
cs.RegisterClientScriptBlock(this.GetType(),
         "MontaDataExclusaoPrazoANS",
         "montaDataSubstituicaoPrestador(" + calDataExclusao.Date + ")", true);


这行不通。没发生什么事。我必须做什么?

如果我这样做,则工作但参数为null:

ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
               "_montaDataSubstituicaoPrestador",
               "montaDataSubstituicaoPrestador(null);", true);


但是,如果改变不起作用。不接受串联字符:

ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
               "_montaDataSubstituicaoPrestador",
               "montaDataSubstituicaoPrestador(" + nova_data + ");", true);


这样,我有此错误:


  参数列表后未捕获到的SyntaxError:缺少)


为什么?

最佳答案

尝试更改您的功能

ClientScriptManager cs = Page.ClientScript;
            cs.RegisterClientScriptBlock(this.GetType(), "MontaDataExclusaoPrazoANS", "montaDataSubstituicaoPrestador(" + calDataExclusao.Date + ")", true)




ClientScriptManager cs = Page.ClientScript;
            cs.RegisterClientScriptBlock(this.GetType(), "MontaDataExclusaoPrazoANS", "montaDataSubstituicaoPrestador('" + calDataExclusao.Date + "')", true)


和为

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "_montaDataSubstituicaoPrestador", "montaDataSubstituicaoPrestador(" + nova_data + ");", true);




ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "_montaDataSubstituicaoPrestador", "montaDataSubstituicaoPrestador('" + nova_data + "');", true);


由于javascript无法将直接参数理解为参数,因此在连接时必须用单引号将其指定。
 希望它能工作

关于javascript - 从后面的代码调用(C#)时Javascript函数不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34138439/

10-12 12:35
查看更多