【问题标题】:passing variable to javascript function fails in asp:dropdownlist在 asp:dropdownlist 中将变量传递给 javascript 函数失败
【发布时间】:2014-10-06 11:56:45
【问题描述】:

目前我有一个按以下方式调用的函数:

<script type="text/javascript">
getCities('<%=BusinessID %>');
</script>

这没有任何问题,现在我想对 asp:dropdownlist 中的 onchange 事件执行相同的操作,如下所示:

<asp:DropDownList ID="ddlAddressSPC" runat="server" clientidmode="Static" AutoPostBack="False" onchange="javascript:getCities('<%=BusinessID %>');" Width="306px" CssClass="txt12NormalLeft" ToolTip="Select State|Province|County" />

但现在 ASP.net 变量没有被评估,而是作为 而不是值传递。

如果我在普通的 HTML 中执行相同的代码,则选择它不是问题。我在这里错过了什么?

【问题讨论】:

  • 将文本 放入变量中,我想在其中包含 BusinessID 的值,我的函数在外部 js 文件中。

标签: javascript asp.net drop-down-menu onchange


【解决方案1】:

您不能直接在服务器元素标记处执行此操作。

你应该在代码后面的某个地方这样做:

ddlAddressSPC.Attributes.Add("onchange", "javascript:getCities('" + BusinessID + "');");

【讨论】:

    【解决方案2】:

    这应该可行。

    <script type="text/javascript">
    
    var currentBusinessIdString = '<%=BusinessID %>';
    
    var currentBusinessId = parseInt(currentBusinessIdString);
    
    getCities(currentBusinessId);
    
    </script>
    

    如果这对您没有帮助,您可以尝试此处列出的一些问题:How do I give JavaScript variables data from ASP.NET variables?

    【讨论】:

      【解决方案3】:

      这样试试

      从标记中删除它

      onchange="javascript:getCities('<%=BusinessID %>');"
      

      并添加page_load

       protected void Page_Load(object sender, EventArgs e)
        {
      
            ddlAddressSPC.Attributes.Add("onchange", "getCities('" + BusinessID + "')");
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-21
        • 2015-05-02
        • 2017-04-30
        • 2018-12-14
        • 2014-10-31
        • 2020-09-02
        • 2018-10-21
        相关资源
        最近更新 更多