本文介绍了JSP单选按钮值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何得到单选按钮值。因此,假设,我有了两个单选按钮的形式。我想获得与键关联的值。不过,我得到空当我尝试。

I was wondering how to get radio button values. So suppose, I have a form that has two radio buttons. I would like to get the value associated with the button. However, I get null when I try to.

表格部分

<form method="post" action="insert.jsp" enctype=text/plain>
<table>
<INPUT TYPE="radio" name="command" value="0">Run<INPUT TYPE="radio" NAME="command" VALUE="1">Walk<BR>

Insert.jsp部分

Insert.jsp portion

String sCommand=(String)request.getParameter("command");
out.println(sCommand);

所以反过来,它打印出空

So in turn, it prints out null

推荐答案

使用 GET 方法,而不是 POST 和您code将运行。 (如果你想用'text / plain的'),也看到@divyabharathi正确的是enctype 给予POST方法的答案。

Use GET method instead of POST and your code will run. (if you want to use 'text/plain') and also see the answer given by @divyabharathi for the correct enctype for POST method.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="get" action="insert.jsp" enctype=text/plain>
<INPUT TYPE="radio" name="command" value="0"/>Run
<INPUT TYPE="radio" NAME="command" VALUE="1"/>Walk
<INPUT TYPE="submit" VALUE="submit" />
</form>
<%
String sCommand = request.getParameter("command");
out.println(sCommand);
%>
</body>
</html>

但我强烈建议你不要在你的JSP中使用 scriplets ,看一看的

这篇关于JSP单选按钮值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 22:18