我正在修改一个老开发者的代码,我正在尝试做一个预订表格。
我查看了整个代码,唯一称为预订的是表单的名称和 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 thename
content attributes of all theapplet
, exposedembed
,form
,iframe
,img
, and exposedobject
elements in theDocument
that have non-emptyname
content attributes, and the values of theid
content attributes of all theapplet
and exposedobject
elements in theDocument
that have non-emptyid
content attributes, and the values of theid
content attributes of all theimg
elements in theDocument
that have both non-emptyname
content attributes and non-emptyid
content attributes.
这些属性的值是 name
或 id
来自的元素。
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 alla
,applet
,area
,embed
,form
,frameset
,img
, andobject
elements in the active document that have a non-emptyname
content attribute, and- the value of the
id
content attribute of any HTML element in the active document with a non-emptyid
content attribute.
这些属性的值又是 name
或 id
来自的元素。
在这两种情况下,这是 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/