java - 如何在jsp中从数据库中的下拉值上创建多个依赖的TextBox

标签 java javascript jquery ajax jsp

请我需要您的帮助,了解如何在 Jsp 中从数据库中生成下拉值的依赖文本框。下面的代码可以很好地处理一个文本框。但我不知道如何制作多个依赖的文本框。所以请帮助我。

谢谢。

tender.jsp:  

<%
     Connection conn = null;
     Statement st = null;
     ResultSet rs = null;*
     conn = JDBCConnectionUtil.getConnection();

     if(conn != null)
     {
        try
        {
            String woodSQL = "select fsa_wood_type_master_wood_type, fsa_wood_type_master_wood_weight,fsa_wood_type_master_wood_length,fsa_wood_type_master_wood_width,fsa_wood_type_master_wood_standard_rate from fsa_wood_type_master";

st = conn.createStatement();
  rs = st.executeQuery(woodSQL);
                   }
      catch(SQLException e)
      {
          e.printStackTrace();
      }
      }
  %>

        <td>
    <select id="woodType" onchange="populateWoodId();">

    <%while(rs.next()){ %>

            <option value="<%=rs.getString(1) %>"><%=rs.getString(2) %></option>
        <%} %>
    </select>
    <input id="woodHeight" type="text" value="" />
</td>

}

JavaScrip:

function populateWoodId(){
    var selectBox = document.getElementById('woodType');

    /* selected value of dropdown */
    var woodHeight = selectBox.options[selectBox.selectedIndex].value;

    /* selected value set to input field */
    document.getElementById('woodHeight').value = woodHeight; 
}

最佳答案

像这样更改选项标记行:

<option value='<%=rs.getString(1)+"#"+rs.getString(2)+"#"+rs.getString(3)+"#"+rs.getString(4)+"#"+rs.getString(5) %>'><%=rs.getString(2) %></option>

并在你的 JavaScript 中这样做:

function populateWoodId(){
    var selectBox = document.getElementById('woodType');

    /* selected value of dropdown */
    var woodProp = selectBox.options[selectBox.selectedIndex].value;

    /* selected value set to input field */
    var splitVal=woodProp.split("#",5);
    document.getElementById('woodHeight').value = splitVal[0]; 
    document.getElementById('woodHeight1').value = splitVal[1]; 
    document.getElementById('woodHeight2').value = splitVal[2]; 
    document.getElementById('woodHeight3').value = splitVal[3]; 
    document.getElementById('woodHeight4').value = splitVal[4]; //five different textboxes

}

关于java - 如何在jsp中从数据库中的下拉值上创建多个依赖的TextBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29179967/

相关文章:

javascript - .slideToggle() 在元素到达页面顶部时隐藏元素,动画出现缩短

javascript - 在 chrome 中禁用右键单击菜单

javascript - jQuery 如何不断检查元素是否隐藏/可见?

java - 如何使用Scala(Spark)API从HBase读取列名称和值?

OS X JREs 6 和 7 : any workaround? 之间的 Java applet 安全模型不兼容

javascript - 使用 Angular 从 Google Feed API 获取图像

javascript - 无法使用 jquery 获取 anchor 标记值

javascript - 在 jquery 中使用 url for 时不允许使用方法

java - 任何允许组合条目的高效 BlockingQueue 实现?

java - OAuth token 在浏览器上未经授权