eclipse 如何在jsp中获取禁用文本字段的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7606251/
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-09-19 17:04:48  来源:igfitidea点击:

How to get the value of a disabled text field in jsp

javahtmleclipsejsp

提问by Anuj Balan

I am having a dropdown box and a 5 textfields( all disabled). I am entering data into textfield by using javascript, from the dropdown(what ever value is present in the dropdown, goes into the text fields).

我有一个下拉框和 5 个文本字段(全部禁用)。我正在使用 javascript 从下拉列表中将数据输入到文本字段中(下拉列表中存在的任何值都会进入文本字段)。

Now, when the submit button is clicked, I want to get the value from this text field in the action class(java). On testing, I was getting "null" [getParameterValues("textfieldname") is what I have done].

现在,当单击提交按钮时,我想从操作类(java)中的此文本字段中获取值。在测试中,我得到了“null”[getParameterValues("textfieldname") 是我所做的]。

When I removed the disabled, I was getting the value. So, how can I get the value while the disabled, is applied to the text field ?

当我移除残疾人时,我得到了价值。那么,如何在禁用时获取值,应用于文本字段?

回答by Harry Joy

Instead of disable them make them readonly.

而不是禁用它们使它们只读。

<input type="text" name="nameOfTextField" readonly="readonly" />

回答by francesco.s

if you want the field to be disabled you can use an hidden input like this:

如果您希望该字段被禁用,您可以使用这样的隐藏输入:

<input type="text" id="nameVisible" disabled="disabled" />
<input type="hidden" name="nameObj" id="nameObj"/>

when you load page, you set value in both fields via DOM
in this way you'll see the input disabled on the page, and you'll get the hidden value when you submit it.

当您加载页面时,您通过 DOM 在两个字段中设置值,
这样您将看到页面上的输入被禁用,并且在您提交时将获得隐藏值。

回答by Santafe

If you still want text fields to be disabled, double them: one with disabled, other with hidden type.

如果您仍然希望禁用文本字段,请将它们加倍:一个禁用,另一个隐藏类型。

Example:

例子:

<select name="selectedItem">
    <option value="1" selected>A</option>
    <option value="2" selected>B</option>
</select>

<input name="iname" value="${selectedItem}" disabled />
<input name="inameh" value="${selectedItem}" type="hidden" />

Now the inamefield will be visible at site (disabled), and you can get the choosen value from inameh(hidden) with:

现在iname字段将在站点上可见(已禁用),您可以使用以下命令从inameh(隐藏)中获取选择的值:

javascript: getParameterValues("inameh")
java: request.getParameterValues("inameh")