0

I have two drop down lists with the names meterid and subzone and also i have 2 radio buttons in my application.If the user selects radio button1 , meterid dropdown list should be displayed. If the user selects radio button2, subzone dropdown list should be displayed. i will get the dropdown values from mysql tables.

i tried the following code:

function select1() 

{ document.getElementById("meter_id").style.display="block"; document.getElementById("subzone_list").style.display="none"; }

function select2() {
     document.getElementById("subzone_list").style.display="block";
     document.getElementById("meter_id").style.display="none";             
    }  

    document.getElementById("midradio").onclick = select1;
    document.getElementById("subzonerd").onclick = select2;

    </script>
    The following code is for Meterid dropdown lisT:
    <tr><td  width="15%" class="options0" id="tdSearchBy" colspan="5">

    <input name="r1" value="MeterID" onclick="" type="radio" id="midradio"><font size="4">Meter ID</font></td>

    <%
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/suwatermeter","root","sumith");
        sqlQuery="select distinct Meterid from Meter_List";
        st=con.createStatement();

         rs=st.executeQuery(sqlQuery);
    %>


    <td  width="20%" class="options0" colspan="5">
    <select style="WIDTH: 150px" selected="true" name="meterid" id="meter_id"  onChange="this.form.submit()" style='display:none;'>
    <option>Select Meter ID</option>
    <% while(rs.next()) {%>
    <option value="<%=rs.getInt("MeterID")%>"><%=rs.getInt("MeterID")%></option>

    <% } 

    rs.close();
    st.close();
    con.close();
    %>

    </select>

    </td></tr>
    The following code is for Subzone dropdown list:
    <tr><td class="options1" id="tdSearchBy" colspan="5">

    <input name="r1" value="Subzone"  onclick="searchOptions(3)" value="subzone" type="radio" checked="checked" id="subzonerd"><font size="4">Sub zone</font></td>

     <%

        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/suwatermeter","root","sumith");

        String getZoneName=request.getParameter("zonelist");
        sqlQuery="select  distinct Subzone from Meter_List where Zone='"+getZoneName+"'" ;
        st=con.createStatement();
        rs=st.executeQuery(sqlQuery);
    %>
    <td  width="20%" class="options0" id="tdSearchBy" colspan="5">
    <select style="WIDTH: 150px" name="subzonelist" id="subzone_list" onchange="this.form.submit()">
    <option value="SZ">Select a Sub Zone</option>


    <% while(rs.next()){ %>
        <option value="<%=rs.getString("Subzone")%>"><%=rs.getString("Subzone")%></option>



    <%  } 

    rs.close();
    st.close();
    con.close();
    %>
    </select>

please help me.

4

1 回答 1

0
  1. 从您的问题中不清楚是什么问题。(期望它没有按您的意愿工作)
  2. 这是一个javascript问题而不是jsp问题,所以标签是错误的。
  3. 我认为你有一个问题,你试图在 DOM 完成加载之前通过它们的 ID 获取元素。

    document.getElementById("midradio").onclick = select1; document.getElementById("subzonerd").onclick = select2;

您应该将这两行放在 HTML 的末尾或在 DOM 准备好后调用的函数中阅读: http ://www.w3schools.com/jsref/event_body_onload.asp

您的“subzonerd”输入元素已经在 html 中设置了 onclick 函数,这可能会在以后产生另一个问题。

于 2012-11-30T13:47:46.450 回答