java - getting hidden fields value - Stack Overflow

I am working on a Struts2 application. I am setting the value of a hidden field in JSP with the purpose

I am working on a Struts2 application. I am setting the value of a hidden field in JSP with the purpose to access it by JavaScript.

My JSP code:

<s:iterator value="collegelist">
    <tr>
        <td align="center"><s:property value="collegename"/></td>
        <s:hidden name="hiddenname" key="collegename" />  
    </tr> 
</s:iterator>

My JS code:

var myForm = document.frmAction;
var text = myForm.hiddenname.value;
alert("hidden field text is:" + text);

The alerts shows a blank value.

What is the cause and how can I solve this?

I am working on a Struts2 application. I am setting the value of a hidden field in JSP with the purpose to access it by JavaScript.

My JSP code:

<s:iterator value="collegelist">
    <tr>
        <td align="center"><s:property value="collegename"/></td>
        <s:hidden name="hiddenname" key="collegename" />  
    </tr> 
</s:iterator>

My JS code:

var myForm = document.frmAction;
var text = myForm.hiddenname.value;
alert("hidden field text is:" + text);

The alerts shows a blank value.

What is the cause and how can I solve this?

Share Improve this question edited Mar 1, 2011 at 13:48 BalusC 1.1m376 gold badges3.7k silver badges3.6k bronze badges asked Mar 1, 2011 at 13:27 singhsingh 3091 gold badge15 silver badges27 bronze badges 7
  • Did you incorrectly spell document.frmAction? – Jeremy Commented Mar 1, 2011 at 13:29
  • it is correct: var myForm = document.frmAction; – singh Commented Mar 1, 2011 at 13:34
  • @singh: alert(myForm); does it shows null? – Dead Programmer Commented Mar 1, 2011 at 13:36
  • @ Suresh S: alert(myForm); shows: [object HTMLFormElement] – singh Commented Mar 1, 2011 at 13:38
  • 1 does your hidden element even contain any value.. i tried this and it works jsbin./ajajo4/edit – Shekhar_Pro Commented Mar 1, 2011 at 13:46
 |  Show 2 more ments

5 Answers 5

Reset to default 3

Try

element = document.getElementsByName("hiddenname");
alert(element[0].value);

You generate multiple fields having the same name, since your code is inside a s:iterator tag. You should obviously have such a loop in your Javascript as well :

var hiddenFields = document.getElementsByName("hiddenname");
for (var i = 0; i < hiddenFields.length; i++) {
    alert("hidden field text is::" + hiddenFields[i].value);
}

Also, verify the the value is not blank in the generated HTML, and that the hidden fields'a name is hiddenname.

I tried your code and it surely works.. problem is somewhere in your server code itself..

Look here: http://jsbin./ajajo4/2/edit

Make sure you have only one form with the name "frmAction" and only one hidden field with the name "hiddenname". If you have multiple, you'll get an array instead of a single value.

The root of the problem is that you are inside of an iterator. Struts updates the name for you in order to correctly hook everything up. If you pull up your page and view source, your hidden field will probably look something like this:

<input type="hidden" name="collegelist[0].hiddenname" value="thename"/>

Regardless, if you want the retrieval by name to work, do not trust the name that you supply to a struts tag. Always pull up the generated source and look at what name the field actually has.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745109214a4611745.html

相关推荐

  • java - getting hidden fields value - Stack Overflow

    I am working on a Struts2 application. I am setting the value of a hidden field in JSP with the purpose

    15小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信