javascript 如何使用按钮将值设置为隐藏属性?

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

How to set value to a hidden property with a button?

javascript

提问by brain_damage

I have the following files:

我有以下文件:

view.jsp

视图.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

我的FooAction.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 booand to invoke MyFooActionwith correct value of boo?

问题是如何changeBoo()在 Javascript 中实现以更改 的值boo并以MyFooAction正确的值调用boo

回答by Pat

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.

首先,将您的按钮更改为type="submit"。这将负责为您提交表格。请注意changeBoo()现在如何为您的 onclick 属性返回一个值。如果您的函数返回,这将提交表单true

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

此外,您需要向id隐藏字段添加一个属性,以便您可以轻松地从 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:

那么这只是创建 javascript 函数的问题:

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

回答by Buhake Sindi

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

PS在您的 上<html:form>...</html:form>,请确保您可以提交表单。这通常通过添加<html:submit>.

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

现在,回到您的问题,您的 Javascript 函数将是这样的(假设您ActionForm在 struts-config.xml 上指定的名称是 "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".

请记住,Struts 将布尔值转换为“true”或“false”、“yes”或“no”、“0”或“1”。