最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to set value to a hidden property with a button? - Stack Overflow

programmeradmin3浏览0评论

I have the following files:

view.jsp

<@ page import=...
<bean:define id="mForm" name="myForm" type="MyForm"/>
<html:form action="MyFoo" method="post" styleId="myForm" enctype="multipart/form-data">
<html:hidden property="boo"/>
<input type="button" value="Press me" onclick="javascript:changeBoo()"/> 
</html:form>

MyForm.java

class MyForm {
    private boolean boo;
    public void setBoo(boolean boo){
        this.boo = boo;
    }
    public boolean getBoo(){
        return this.boo;
    }
}

MyFooAction.java

public class MyFooAction extends BaseAction {

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ActionForward aForward = null;
        String forward = "success";

        try {
            MyForm myForm = (MyForm) form;
            String boo = (String)request.getParameter("boo");
            if(boo.equals("true")){
                System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>DONE");
            }
            else {
                //some code here
            }
            aForward = mapping.findForward(forward);
        }
        catch (Exception e) {
            throw new Exception();
        }

        return aForward;
    }
}

The question is how to implement changeBoo() in Javascript in order to change the value of boo and to invoke MyFooAction with correct value of boo?

I have the following files:

view.jsp

<@ page import=...
<bean:define id="mForm" name="myForm" type="MyForm"/>
<html:form action="MyFoo" method="post" styleId="myForm" enctype="multipart/form-data">
<html:hidden property="boo"/>
<input type="button" value="Press me" onclick="javascript:changeBoo()"/> 
</html:form>

MyForm.java

class MyForm {
    private boolean boo;
    public void setBoo(boolean boo){
        this.boo = boo;
    }
    public boolean getBoo(){
        return this.boo;
    }
}

MyFooAction.java

public class MyFooAction extends BaseAction {

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ActionForward aForward = null;
        String forward = "success";

        try {
            MyForm myForm = (MyForm) form;
            String boo = (String)request.getParameter("boo");
            if(boo.equals("true")){
                System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>DONE");
            }
            else {
                //some code here
            }
            aForward = mapping.findForward(forward);
        }
        catch (Exception e) {
            throw new Exception();
        }

        return aForward;
    }
}

The question is how to implement changeBoo() in Javascript in order to change the value of boo and to invoke MyFooAction with correct value of boo?

Share Improve this question edited Aug 17, 2010 at 13:05 Marcel Korpel 21.8k6 gold badges62 silver badges80 bronze badges asked Aug 17, 2010 at 12:09 brain_damagebrain_damage 9856 gold badges21 silver badges29 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

First, change your button to type="submit". That will take care of submitting the form for you. Notice how changeBoo() now returns a value for your onclick attribute. This will submit the form if your function returns true.

Also, you'll need to add an id attribute to your hidden field so that you can easily get a reference to it from javascript:

<html:hidden property="boo" id="booId" />
<input type="submit" value="Press me" onclick="return changeBoo();"/> 

Then it's just a matter of creating the javascript function:

function changeBoo(){  
   var boo = document.getElementById('booId');    
   boo.value = 'The new value';
   return true;
}

PS On your <html:form>...</html:form>, make sure you have a way to submit a form. This is usually done by adding <html:submit>.

Now, to e back to your question, your Javascript function will be like this (assuming that your ActionForm name specified on struts-config.xml is "myForm").

fumction changeBoo() {
  var boo = document.myForm.boo;
  if ("true" == boo.value.toLowerCase() || "yes" == boo.value.toLowerCase() || "1" == boo.value.toLowerCase()) {
     boo.value = "false";
  } else {
     boo.value = "true";
  }
}

Bear in mind that Struts converts boolean values to "true" or "false", "yes" or "no", "0" or "1".

发布评论

评论列表(0)

  1. 暂无评论