jQuery 取得选中的option对象
jQuery 获得选中的option对象
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'Jquery006.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <script type="text/javascript" src="js/jquery-1.6.1.js"></script> <!-- 获得选中的option对象 --> <script type="text/javascript"> $(document).ready(function() { $("#s1").change(function() { var str = ""; //select :selected 注意中间有空格,少了空格就没有效果了 $("select :selected").each(function() { str += $(this).text() + ','; }); $("#div1").html('<b>' + str + '</b>'); }); }); </script> </head> <body> <form> <select id="s1" multiple="multiple"> <option>苏州</option> <option selected="selected">上海</option> <option>南京</option> <option>丹阳</option> <option>常州</option> <option>杭州</option> <option>无锡</option> </select> <div id="div1"></div> </form> </body> </html>