input标签type写成radio单选。后面的文字自己添加,男女两个的input都要用相同的name值,因为只能选择一个。
<form action="/example/html/form_action.asp" method="get"><input type="radio" name="sex" value="male" /> Male<br />
<input type="radio" name="sex" value="female" /> Female<br />
<input type="submit" value="Submit" />
</form>
1、新建一个html文件,命名为test.html。
2、在test.html文件内,引入jquery.min.js库文件,成功加载该文件,才能使用jquery中的方法。
3、在test.html文件内,在p标签内,使用input标签创建两个radio选项,分别是男,女两个选项。
4、在test.html文件内,使用button标签创建一个按钮,按钮名称为“获取选中的值”。
5、在test.html文件中,给button按钮绑定onclick点击事件,当按钮被点击时,执行getradio()函数。
6、在js标签中,创建getradio()函数,在函数内,通过input元素的“:checked”选择器获得已选中的radio对象,使用val()方法获得radio的值。最后,使用alert()方法将值输出。
表单:<form action="doservlet" method="post">
姓名:<input name="user_name"><br>
性别<input type="radio" value="man" name="sex">男 <input type="radio" value="women" name="sex">女<Br />
爱好:<input type="check" name="likes" value="运动" />运动
<input type="check" name="likes" value="读书" />读书
<input type="check" name="likes" value="音乐" />音乐
<input type="check" name="likes" value="书法" />书法
</form>
servlet:
iimport java.io.IOException
import java.io.PrintWriter
import javax.servlet.ServletException
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
public class doservlet extends HttpServlet {
/**
* Constructor of the object.
*/
public doservlet() {
super()
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy()// Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html")
PrintWriter out = response.getWriter()
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">")
out.println("<HTML>")
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>")
out.println(" <BODY>")
out.print("This is ")
out.print(this.getClass())
out.println(", using the GET method")
out.println(" </BODY>")
out.println("</HTML>")
out.flush()
out.close()
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html")
PrintWriter out = response.getWriter()
String name = null
String sex = null
String likes[] = null
name=request.getParameter("user_name")
sex=request.getParameter("sex")
likes=request.getParameterValues("likes")
request.getSession().setAttribute("user_name", name)
request.getSession().setAttribute("sex", sex)
request.getSession().setAttribute("like",likes)
//把请求过来的数据放在session里
response.sendRedirect("目的页")
//在目的页中通过session的 getAttribute方法取出来即可
out.flush()
out.close()
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)