getElementById返回Null但是有元素

时间:2014-02-06 15:48:11

标签: javascript jquery asp.net

您好我有3个下拉列表,我使用<select>标记实现。在第一个ddl我关联了以下javascript函数

function aggiorna_provincia(tipo_pannello) {
    var objregione; 
    objregione = document.getElementById("<%=regione_residenzaIDropDownList.ClientID%>");
    id_regione = objregione.options[objregione.selectedIndex].value;

    if (id_regione != 0) {
        $.ajax({
            url: "../jquery/update1.aspx",
            type: "get",
            data: "cc=" + id_regione + "&tp=" + tipo_pannello,
            success: function (response) {
                $("#div_provinciaI").html(response);
                $("#<%=provincia_residenzaIDropDownList.ClientID%>").focus();
            },
            error: function () {
                alert("errore di connessione");
            }
        });
    }
    break;      
}

并且update1页面是

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim codice As Integer = Integer.Parse(Request.QueryString("cc").ToString())
    Dim tipo_pannello As Integer = Integer.Parse(Request.QueryString("tp").ToString())
    Dim regione As New List(Of regioni)

    Using db_antiriciclaggio As New db_antiriciclaggioEntities
        Try
            regione = (From tab_r In db_antiriciclaggio.regioni Where tab_r.IDNazione = codice Select tab_r).ToList()
        Catch ex As Exception
        End Try
    End Using
     Response.Write("<div id='div_regioneI' style='text-align:left'>")
    Response.Write("<select id='regione_residenzaIDropDownList' runat='server' onchange='aggiorna_provincia(1)'>")

    If (regione.Count = 0) Then
        Response.Write("<option value='0'>Scegli prima lo stato</option>")
    Else
        Response.Write("<option value='0'>Scegli la regione</option>")
    End If
    For Each temp In regione
        Response.Write("<option value='" & temp.idregione & "'>" & temp.nomeregione & "</option>")
    Next

    Response.Write("</select>")
    Response.Write("</div>")
End Sub 

但是当我在第二个ddl上运行aggiorna_provincia()函数(更新第三个ddl的类似函数)时,当我尝试在javascript函数中找到ddl时,我获得了一个空对象

1 个答案:

答案 0 :(得分:0)

我想“&lt;%= regione_residenzaIDropDownList.ClientID%&gt;”和'regione_residenzaIDropDownList'不一样。

顺便说一句:你的response.write(...)方法看起来很难维护你的代码。你应该在模板中使用更多的asp。