单击按钮时,JSP从数据库填充下拉列表 [英] JSP populate dropdown list from database on button click

查看:71
本文介绍了单击按钮时,JSP从数据库填充下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
 <%@page contentType="text/html" pageEncoding="UTF-8"%>
 <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                <title>JSP Page</title>
            </head>
            <body>
                 <sql:setDataSource var="db"
                                   driver="com.mysql.jdbc.Driver"
                                   url="jdbc:mysql://localhost/dbase"
                                   user="root"
                                   password="1asulxayev" />
                <sql:query var="select" dataSource="${db}">
                    select * from dtable
                </sql:query>
                   <select name="lst">
                    <c:forEach var="result" items="${select.rows}"> 
                      <option>${result.name}</option>
                   </c:forEach>
                 </select>    
              <input type="submit" value="Fill" name="btn">
                 </body>
        </html>

这是页面加载下拉列表填充的时间.但是我想当按钮单击时填充下拉列表

This time when page load dropdown list populate. but i want when button click populate dropdown list

推荐答案

以下是使用AJAX的示例代码.有关更多信息,请阅读嵌入式注释.

Here is the sample code using AJAX. For more info read inline comments.

Servlet:

使用doGet()方法从数据库中获取数据,然后简单地将逗号分隔的字符串写入HTTP响应并将其刷新到客户端.

Fetch the data from the database in doGet() method and simply write a comma separated string the HTTP response and flush it to the client.

HTML:

<head>
<script type="text/javascript">
    $(document).ready(
            function() { // When the HTML DOM is ready loading, then execute the following function...
                $('.btn-click').click(
                        function() { // Locate HTML DOM element with ID "somebutton" and assign the following function to its "click" event...
                            $.get('myServletURL', function(responseJson) { // Execute Ajax GET request on URL of "someservlet" and execute the following function with Ajax response JSON...
                                //alert(responseJson);
                                var $select = $('#maindiv'); // Locate HTML DOM element with ID "someselect".
                                $select.find('option').remove(); // Find all child elements with tag name "option" and remove them (just to prevent duplicate options when button is pressed again).
                                var items = responseJson.split(',');

                                for ( var i = 0; i < items.length; i++) {
                                    $('<option>').val(items[i]).text(items[i])
                                            .appendTo($select); // Create HTML <option> element, set its value with currently iterated key and its text content with currently iterated item and finally append it to the <select>.
                                }
                            });
                        });
            });
</script>

</head>
<body>
    <select id="maindiv" style="width: 300px;"></select>
    <input type="button" class="btn-click" id="best" value="check" />
</body>

这篇关于单击按钮时,JSP从数据库填充下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆