select自定义属性值
1、问题背景
下拉框可以传递值和内容,不过有时为了传值,还需要连带其他的值也一起传过来;如果用title属性,鼠标移到下拉框上方会显示出来,这样就会导致被暴露出来。所以,为了安全和传值方便,可以利用自定义属性。
2、实现源码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% 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>自定义下拉框属性值</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="<%=basePath%>/js/jquery-2.2.3.js"></script> <script type="text/javascript"> $(function(){ $("#sel").change(function(){ var sel = $("#sel option:selected").val(); var name = $("#sel option:selected").text(); var child = $("#sel option:selected").attr("child"); alert("下拉框的值:"+sel+"\n"+"下拉框的内容:"+name+"\n"+"下拉框属性值child:"+child); }); }); </script> </head> <body> <div id="div-select"> <select id="sel" style="width:200px;"> <option value="1" child="2016010101">篮球</option> <option value="2" child="2016010102">足球</option> <option value="3" child="2016010103">铅球</option> <option value="4" child="2016010104">羽毛球</option> <option value="5" child="2016010105">乒乓球</option> <option value="6" child="2016010106">网球</option> </select> </div> </body> </html>
3、实现结果