javascript - 这是什么 : document. reservation.submit();应该做的?

标签 javascript jquery html

我正在修改一个老开发者的代码,我正在尝试做一个预订表格。

我查看了整个代码,唯一称为预订的是表单的名称和 ID。

表格谁是流行的:display:none ...

所以两个问题合二为一:首先到底应该做什么

document.reservation.submit(); 是否应该通过他的名字获取表格?

它不应该是 document.getElementById('reservation').submit() 之类的东西吗?

我的第二个问题是:如果所有值都设置为 display:none,如何发送表单我很难做到,如果你想隐藏它们,你应该使用 hidden属性(property)...

我需要一些关于这些人的帮助 :)

更好理解的形式:

<form name='reservation' action='http://xxxx/reservationFormAction.to' method="POST" id="reservation">
    <input type="hidden" id="productLive" name="product" value="{$product.info.code}"/>
    <input type="hidden" name="complementaryParameters" value=""/>    
    <input type="text" name="depCityCode" id="depCityCode" style="display:none"  />
    <input type="text" name="dateDep" id="dateDep" style="display:none" />
    <input type="text" name="nightDuration" id="nightDuration"  style="display:none" />
    <input type="text" name="dayDuration" id="dayDuration"  style="display:none" />
    <input type="text" name="provider" value="{$product.tourOperator.code}" style="display:none" />
    <input type="text" id="toProduct" name="toCode" value="{$product.info.toProductCode}" style="display:none" />
    <input type="text" name="catalogCode" value="{$product.info.code}" style="display:none" />
    {if $ecall}
    <input type="text" name="reservationProfileChannelCode" value="ECALL" style="display:none" />
    {else}
    <input type="text" name="reservationProfileChannelCode" value="ADV" style="display:none" />
    {/if}
    <input type="text" name="nbAdults" id="nbAdults" style="display:none" />
    <input type="text" name="nbChildren" id="nbChildren" style="display:none" />
    <input type="text" name="nbBabies" id="nbBabies" style="display:none" />
    <input type="text" name="productUrl" id="productUrl" style="display:none" value="http://www.xxxx.com/{$product.slug}_{$product.info.code}.html" />
    <input type="text" name="homeUrl" id="homeUrl" style="display:none" value="http://www.xxxx.com" />
    <span id="ageChild" style="display:none"></span>
<div class="update-search clearfix">

最佳答案

document.reservation 获取具有 name reservation 的表单的 HTMLFormElement。然后调用 submit 提交表单(不触发 submit 事件)。

那么为什么不document.getElementById呢?这可以工作,但是 document.reservation 可以工作,因为 document 对象会自动获得在其上创建的各种属性,包括通过它们的形式引用表单的属性名称。这包含在 §3.1.3 of the HTML5 spec 中*(你必须向下滚动一点):

The Document interface supports named properties. The supported property names at any moment consist of the values of the name content attributes of all the applet, exposed embed, form, iframe, img, and exposed object elements in the Document that have non-empty name content attributes, and the values of the id content attributes of all the applet and exposed object elements in the Document that have non-empty id content attributes, and the values of the id content attributes of all the img elements in the Document that have both non-empty name content attributes and non-empty id content attributes.

这些属性的值是 nameid 来自的元素。

window 对象还获取每个具有 id 的元素的属性,如 described here :

The supported property names at any moment consist of the following, in tree order, ignoring later duplicates:

  • the browsing context name of any child browsing context of the active document whose name is not the empty string,
  • the value of the name content attribute for all a, applet, area, embed, form, frameset, img, and object elements in the active document that have a non-empty name content attribute, and
  • the value of the id content attribute of any HTML element in the active document with a non-empty id content attribute.

这些属性的值又是 nameid 来自的元素。

在这两种情况下,这是 HTML5 规范,它标准化了大多数浏览器以前广泛使用但非标准的做法,广泛用于网页上。


How the form can be sent if all the value are set to display:none I tough it couldn't work and if you want to hide them you shall use hidden property...

最好每个问题问一个问题。

CSS display 属性对是否提交表单域没有任何影响;你可能会想到该领域的 disabled state : 禁用的表单字段确实在提交时被排除在表单之外。

关于javascript - 这是什么 : document. reservation.submit();应该做的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31653276/

相关文章:

javascript - 如何获取 <OPTION> 元素的未 chop 文本值?

javascript - jQuery 事件 trim 对我不起作用

javascript - jQuery 多输入更改

php - AJAX 登录表单 : Undefined PHP variables

javascript - 为什么我的 &lt;script&gt; 标签不执行其中的代码?

javascript - 如何在最后一张幻灯片后停止动画?

javascript - 如何选择与单击按钮相同的 div 的元素

html - 记录屏幕加载时的 html 更改?

javascript - JS 对象和函数更正

javascript - 排序 JavaScript 数组时的奇怪行为