以前每次写一些什么元素被选中,就隐藏什么,获取多选框的值,获取下拉框的值、文本,我就紧张,还是巩固一下吧,之前项目都用过,这里再练习一下
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ include file="/common/common.jsp" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <%@ include file="/common/common-ui.jsp" %> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> <script type="text/javascript"> $(function(){ var obj=document.getElementsByName("fruit"); for(var i=0;i<obj.length;i++){ obj[i].checked=false; } }); function chose(){ $.ajax({ url:'${basePath}/doexercise', dataType:'json', data:"text="+$("#university").find("option:selected").val(),//value /* data:"text="+$("#university").find("option:selected").text(),//text */ success:function(data){ if(data.success){ alert(data.msg); }else{ alert(data.msg); } }, error:function(){ alert("error"); } }); } function showDiv(){ $("#logo").show(); } function hiddenDiv(){ $("#logo").hide(); } function submit(){ alert("dd"); var obj=document.getElementsByName("fruit"); var s=""; for(var i=0;i<obj.length;i++){ if(obj[i].checked){ if(s==""){ s+=obj[i].value; }else{ s+=","+obj[i].value; } } } alert(s); } </script> </head> <body> <center> <select id="university" onchange="chose()"> <option value="1">湖北大学</option> <option value="2">湖南大学</option> <option value="3">重庆大学</option> </select> <button onClick="showDiv()">显示div</button> <button onClick="hiddenDiv()">隐藏div</button> <div id="logo" style="display:none"> 穷且益坚,不坠青云之志! </div> <br> <input type="checkbox" name="fruit" value="1" >苹果 <input type="checkbox" name="fruit" value="2">梨子 <input type="checkbox" name="fruit" value="3">草莓 <input type="checkbox" name="fruit" value="4" >香蕉 <input type="button" onClick="submit()" value="多选提交"> </center> </body> </html>
最后,务必清楚,basePath是如何拿到的,<c:set var="basePath" value="${pageContext.request.contextPath}"/>
啦啦。。。pageContext.request.contextPath