java Struts 1 如何设置复选框默认选中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7567673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:32:40  来源:igfitidea点击:

Struts 1 How to set checkbox default checked

javastruts-1

提问by hendry.fu

I'm making some small changes to an existing project, which was built using Struts 1.2. I need to have a check box on the form checked by default. I'm a newbie on Struts.

我正在对使用 Struts 1.2 构建的现有项目进行一些小的更改。我需要在默认情况下选中表单上的复选框。我是 Struts 的新手。

I understand that I can't set a default value for check boxes on form reset method, in fact it is suggested to set the value to false for checkboxes in reset method. The only way I can think of is to check the POST param, but I don't think this is a good solution.

我知道我无法为表单重置方法中的复选框设置默认值,实际上建议将重置方法中的复选框的值设置为 false。我能想到的唯一方法是检查 POST 参数,但我认为这不是一个好的解决方案。

Is there a correct Struts way to have a default checked checkbox?

是否有正确的 Struts 方法来选中默认复选框?

回答by francesco.s

you have:

你有:

<html:checkbox property="multiRole" value="Y" />

if you want the page rendered with it checked by default, you need to set the property "multiRole" in your action form to "Y"

如果您希望页面呈现时默认选中它,则需要将操作表单中的属性“multiRole”设置为“Y”

回答by Dave Newton

Why can't you set a default value in reset? That's mostly what it's for. You can also set a value in the action that initially displays the form.

为什么不能在reset中设置默认值?这主要是它的用途。您还可以在最初显示表单的操作中设置一个值。

回答by user3744881

alternatively you can use the ,,normal" html tag to solve this problem. This tag can also interact with the form bean via the name parameter. In the form bean you can use a boolean parameter!

或者你可以使用,,normal" html标签来解决这个问题。这个标签也可以通过name参数与form bean交互。在form bean中你可以使用boolean参数!

<input type="checkbox" name="multiRole" checked="checked" />