简体   繁体   English

如何在Chrome中提交JavaScript表单

[英]How to do javascript form submit in chrome

Hi I am working in JavaScript, jQuery and MVC application. 嗨,我正在使用JavaScript,jQuery和MVC应用程序。

document.forms['shipform'].submit();

Here I submit my form like this. 在这里,我提交这样的表格。
Totally I have three form in this page. 在此页面中,我总共有三种形式。
This is my form: 这是我的表格:

<% using (Html.BeginForm("OrderShipments", "Shipments", FormMethod.Post, new { @id = "shipform" })) // Creates <form>
{%>
    <input type="hidden" id="Hiddenid" name="orderId" />
    <input type="submit" value="book" style="display: none" />
<%} %>

It is not working in Chrome, but it works in Firefox. 它不适用于Chrome,但适用于Firefox。

document.forms[0].submit();

If I use forms[0] which form would it take if I have three form in single page? 如果我使用forms[0] ,如果我在一个页面中有三个表单,将采用哪种形式?

The code you have will submit the form based on its name: 您拥有的代码将根据其名称提交表单:

 document.forms['shipform'].submit();
                ^^^ form name not ID

If you want to submit the form by its ID then use: 如果要通过表单ID提交表单,请使用:

 document.getElementById('shipform').submit();

Or jQuery: 或jQuery:

$('#shipform').submit();

If you're using jQuery you can do this: 如果您使用的是jQuery,则可以执行以下操作:

$('#shipform').submit();

Assuming that code outputs a form with ID="shipform" 假设代码输出一个ID="shipform"的表单

<form id="shipform"></form>

You can "get" the form with getElementById() , then submit() it: 您可以使用getElementById() “获取”表单,然后submit()submit()

document.getElementById("shipform").submit();

This should send in your form. 这应该以您的表格发送。
(Assuming the { @id = "shipform" } sets the id on your form to "shipform" ) (假设{ @id = "shipform" }将表单上的id设置为"shipform"

If you use forms[0] , it should use the "first" form on your page, so the one that's loaded first, in your html. 如果您使用forms[0] ,则它应使用页面上的“第一个”表单,因此在html中首先加载的表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM