as:hidden in javascript 的设置值

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

Setting value of a s:hidden in javascript

javascriptstruts2hidden

提问by ITAmateur

I have a JSP with HTML input fields like "username". I have a JavaScript method where I get the value of this field using `document.getElementById('userId').value.

我有一个带有 HTML 输入字段的 JSP,例如"username". 我有一个 JavaScript 方法,我使用 `document.getElementById('userId').value 获取该字段的值。

Now I have a struts2 tag: <s:hidden name="username" />

现在我有一个 struts2 标签: <s:hidden name="username" />

In my JavaScript, before submitting, I need to set the value from the input text field to the Struts 2 hidden tag field. I cannot use the text directly from the input field and I need to set it to the hidden Struts 2 tag.

在我的 JavaScript 中,在提交之前,我需要将输入文本字段中的值设置为 Struts 2 隐藏标签字段。我无法直接使用输入字段中的文本,我需要将其设置为隐藏的 Struts 2 标签。

回答by xrcwrn

First you have to give id to your hidden field then you can set it with jquery or javascript.

首先,您必须为隐藏字段提供 id,然后您可以使用 jquery 或 javascript 设置它。

Following is example to set it with jquery

以下是使用 jquery 设置它的示例

This is your form

这是你的表格

this will set username from jquery;

这将从 jquery 设置用户名;

<script type="text/javascript">
            $(document).ready(function() {
               $(document).on('click', '#s', function(event) {
                    event.preventDefault();
                    var name="Ram";
                    $("#uname").val(name);
                });
            });
</script>

If it is very important data then save it to session or any other scope and get it directly to action dont show it to jsp page

如果它是非常重要的数据,则将其保存到会话或任何其他范围并直接执行操作,不要将其显示到 jsp 页面

回答by anupam

  1. Why cant you directly give the value to hidden field?
  2. Struts2 input tags are finally converted into the normal HTML tags on page rendering, so there is nothing special you need to assign them a value. Just mention a id to the hidden element and assign the value using the javascript like you usually do

    <s:textfield id="mytext" value="yourvalue"/>
    <s:hidden id="myhidden" value=""/>
    
    
    function assignvalue(){
        document.getElementById("myhidden").value=document.getElementById("mytext").value;
    }
    
  1. 为什么不能直接给隐藏字段赋值?
  2. Struts2 的输入标签最终在页面渲染时被转换成普通的 HTML 标签,所以没有什么特别需要给它们赋值的。只需向隐藏元素提及一个 id 并像往常一样使用 javascript 分配值

    <s:textfield id="mytext" value="yourvalue"/>
    <s:hidden id="myhidden" value=""/>
    
    
    function assignvalue(){
        document.getElementById("myhidden").value=document.getElementById("mytext").value;
    }
    

回答by Umesh Awasthi

Struts2 tags only provides some additional functionality to work better with Struts2 flow.They are like helpers which give you some out of the box features to work closely with struts2.

Struts2 标签只提供了一些额外的功能来更好地与 Struts2 流程一起工作。它们就像助手一样,为您提供一些开箱即用的功能,以便与 struts2 密切合作。

In the end when your JSP will be displayed by the browser be it struts2 tag/your custom tags or any other tag library you are using will be converted in to pure HTML which is understood by your browser.

最后,当您的 JSP 将被浏览器显示时,无论是 struts2 标签/您的自定义标签还是您正在使用的任何其他标签库,都将被转换为您的浏览器能够理解的纯 HTML。

So if you have a text-field say

所以如果你有一个文本字段说

<s:textfield name="username" id="username" value=""/>

it will be converted in to HTML at final display like

它将在最终显示时转换为 HTML,例如

<input type="text" name="username" id="username" value="">

Just note that if you will not provide idfor your textfield struts2 will generate the id by itself while converting the tag code to final HTML so if you want your custom id for the textfield/any other field give it explicitly.

请注意,如果您不提供id文本字段,struts2 将在将标签代码转换为最终 HTML 时自行生成 id,因此如果您希望文本字段/任何其他字段的自定义 id 明确给出。

So you have all way to use famous javascript way

所以你有办法使用著名的 javascript 方式

document.getElementById("myhidden")

and do what ever you want to do with the values.

并用这些值做任何你想做的事情。

回答by PD Shah 5382

You can set the value of a textfield to the hidden value on submit button.

您可以将文本字段的值设置为提交按钮上的隐藏值。

Let to show you an example, <script type="text/javascript>

给大家举个例子, <script type="text/javascript>

function setHiddenValue(){

function setHiddenValue(){

document.getElementById('hiddenfield').value = document.getElementById('textfield');}

document.getElementById('hiddenfield').value = document.getElementById('textfield');}

</script>

</script>

<s:hidden name="hiddenfield" id="hiddenfield" />

<s:hidden name="hiddenfield" id="hiddenfield" />

<s:textfield name="textfield" id="textfield" />

<s:textfield name="textfield" id="textfield" />

<input type="submit" onclick="setHiddenValue();" />

<input type="submit" onclick="setHiddenValue();" />