栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

单击按钮时,JSP从数据库填充下拉列表

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

单击按钮时,JSP从数据库填充下拉列表

这是使用AJAX的示例代码。有关更多信息,请阅读内联注释。

Servlet:

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

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" ></select>    <input type="button"  id="best" value="check" /></body>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/466713.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号