简体   繁体   English

Servlet检索预选单选按钮

[英]Servlet retrieve pre selected radio button

I pre select my radio buttons by the following code. 我通过以下代码预先选择单选按钮。 The following inputs wrap in a form that post back to the same servlet. 以下输入以回传到同一servlet的形式包装。

buf.append("<input type=\"radio\" name=\"FTNAME\" value=\""+ FTNAME+ "\" " + (FTNAME.equals("Arial") ? "checked=\"checked\"" : "") + ">Arial &nbsp &nbsp &nbsp");
buf.append("<input type=\"radio\" name=\"FTNAME\" value=\""+ FTNAME+ "\" " + (FTNAME.equals("Serif") ? "checked=\"checked\"" : "") + ">Serif &nbsp &nbsp &nbsp");
buf.append("<input type=\"radio\" name=\"FTNAME\" value=\""+ FTNAME+ "\" " + (FTNAME.equals("SansSerif") ? "checked=\"checked\"" : "") + ">SansSerif <br><br>");

However, when I try to do 但是,当我尝试做

FTNAME = request.getParameter("FTNAME") == null ? "Arial" : request.getParameter("FTNAME"); //Arial as font name default

to get my FTNAME, it always return what it was set from the code above, not my new selection. 为了获得我的FTNAME,它总是返回上面代码中设置的内容,而不是我的新选择。

Any suggestion? 有什么建议吗?

Yes: My suggestion is you'd better read the HTML code produced by your servlet. 是的:我的建议是您最好阅读servlet产生的HTML代码。 You'll find that all three radios have THE SAME VALUE. 您会发现所有三个无线电都具有相同的值。

Because you're setting your value as Arial for all three radio buttons. 因为您将所有三个单选按钮的值都设置为Arial。 So no matter what radio button you choose it will always return you arial. 因此,无论您选择哪种单选按钮,都将始终返回您的里亚尔。

This how your html page would look like: 这就是您的html页面的样子:

<input type="radio" name="FTNAME" value="Arial" checked="checked">
<input type="radio" name="FTNAME" value="Arial">
<input type="radio" name="FTNAME" value="Arial">

And you servlet request.getParameter("FTNAME") would alway return you the value "Arial". 而且您的servlet request.getParameter("FTNAME")会返回值“ Arial”。 You need to change to something like this 您需要更改为这样的内容

buf.println("<input type=\"radio\" name=\"FTNAME\" value=\"Arial\"" + (FTNAME.equals("Arial") ? "checked=\"checked\"" : "") + ">Arial &nbsp &nbsp &nbsp");
buf.println("<input type=\"radio\" name=\"FTNAME\" value=\"Serif\""+ (FTNAME.equals("Serif") ? "checked=\"checked\"" : "") + ">Serif &nbsp &nbsp &nbsp");
buf.println("<input type=\"radio\" name=\"FTNAME\" value=\"SansSerif\""+ (FTNAME.equals("SansSerif") ? "checked=\"checked\"" : "") + ">SansSerif <br><br>");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM