javascript 页面加载时提交表单 - struts 2

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

Submit a form when page loads - struts 2

javascriptformsstruts2form-submit

提问by Ved

I want to submit a s:form when page loads. here is my code :

我想在页面加载时提交为:表单。这是我的代码:

<s:form id="login_form" action="" method="post">
<s:textfield label="User name" id="login_form_username"
            name="username" value="abc.xyz" cssClass="txtbox"
            required="true" />
        <s:password label="Password" id="login_form_password" name="password"
            value="pass" cssClass="txtbox" required="true" />
        <s:submit value="Login" />
    </s:form>

Actually as shown in above code, I will set uname and pwd dynamically and then want to submit this form directly using javascript.

实际上如上面的代码所示,我会动态设置uname和pwd,然后想直接使用javascript提交此表单。

I tried this :

我试过这个:

<script type="text/javascript"> 
      document.forms.login_form.submit();
</script>

but had no success.

但没有成功。

Please help... Thanks..

请帮助...谢谢...

采纳答案by coding_idiot

<s:form id="login_form" action="" method="post">

As you can see the action to which the form will be submitted is missing. How will the above javascript know on which action to submit this form to. Try filling in a action name.

如您所见,表单将提交到的操作丢失了。上面的javascript如何知道将此表单提交给哪个操作。尝试填写操作名称。

回答by Butani Vijay

you can use Jquery code:

您可以使用 Jquery 代码:

document.ready(function(){
  $("#login_form").submit();
});

dont forget to specify action attribute of form.

不要忘记指定表单的 action 属性。

Note: to use jquery you need to add jquery library.

注意:要使用 jquery,您需要添加 jquery 库。

回答by Arun

document.forms.login_form.submit()throwing undefined error.instead of that you can use document.forms[0].submit()

document.forms.login_form.submit()抛出未定义的错误。而不是你可以使用 document.forms[0].submit()

you can use onload attribute for page loads works

您可以使用 onload 属性进行页面加载工作

<script type="text/javascript"> 
    function submit(){
      document.forms[0].submit();
    }
</script>

  <body onload="submit()">
  <s:form id="login_form" action="" method="post">
       <s:textfield label="User name" id="login_form_username"
            name="username" value="abc.xyz" cssClass="txtbox"
            required="true" />
        <s:password label="Password" id="login_form_password" name="password"
            value="pass" cssClass="txtbox" required="true" />
        <s:submit value="Login" />
    </s:form>

 </body>

回答by Raki

If you want to submit through java script means you have to give code like this eg:

如果你想通过java脚本提交意味着你必须提供这样的代码,例如:

If your from name is *login_form*in java script then you can give like this

如果你的名字在java脚本中是*login_form*,那么你可以像这样给出

function submitform()
{
    document.forms["login_form"].submit();
}


<s:form id="login_form" name="login_form" action="your action name" method="post">
<s:textfield label="User name" id="login_form_username"
            name="username" value="abc.xyz" cssClass="txtbox"
            required="true" />
        <s:password label="Password" id="login_form_password" name="password"
            value="pass" cssClass="txtbox" required="true" />
        <s:submit value="Login" onClick="submitform()"/>
</s:form>

Here you should remember 2 important points:

在这里你应该记住2个重要的点:

1)without action name it will not work because, for going to the struts.xml you need action name right in your code i didn't see the action name.

2)Secondly, in your code you written some java script code but, you not called that function name either through submit button nor through body onload function. I will assume like for posting purpose you did this coding k that is not the problem.

1)如果没有动作名称,它将无法工作,因为要转到 struts.xml,您需要在代码中正确使用动作名称,我没有看到动作名称。

2)其次,在您的代码中,您编写了一些 java 脚本代码,但是,您既没有通过提交按钮也没有通过主体 onload 函数调用该函数名称。我假设为了发布目的,你做了这个编码 k 这不是问题。

what i was showed above code that is one way. Another way is without submit button your code should run means, you can type like this in a jsp page

我在上面显示的代码是一种方式。另一种方法是没有提交按钮您的代码应该运行意味着,您可以在jsp页面中输入这样的内容

<META HTTP-EQUIV="Refresh" CONTENT="1;URL=youractionname.action">

you have to write this code before body only then your jsp page works like what you want.

您必须在 body 之前编写此代码,然后您的 jsp 页面才能像您想要的那样工作。

Here i used for both the codes action name(youractionname)means without action name in struts2 page won't go to the action class

在这里,我用于两个代码动作名称(您的动作名称)意味着在 struts2 页面中没有动作名称将不会转到动作类

Bottom Line:Action name is important in struts2

底线:动作名称在 struts2 中很重要