使用 <form> 之外的按钮提交表单标签

2022-01-30 00:00:00 html

说我有:

<form method="get" action="something.php">
    <input type="text" name="name" />
</form>

<input type="submit" />

如何在表单之外使用提交按钮提交该表单,我认为在 HTML5 中有一个用于提交的操作属性,但我不确定这是否完全跨浏览器,如果不是有什么办法吗?

How do I go about submitting that form with that submit button outside of the form, I think in HTML5 theres an action attribute for the submit but I'm not sure if thats fully cross-browser and if it isn't is there anyway to do this?

推荐答案

更新:在现代浏览器中,您可以使用 form 属性来做到这一点.

Update: In modern browsers you can use the form attribute to do this.

据我所知,如果没有 javascript,您将无法做到这一点.

As far as I know, you cannot do this without javascript.

这就是规范所说的

用于创建控件的元素通常出现在 FORM 中元素,但也可能出现在 FORM 元素声明之外当它们用于构建用户界面时.这在关于内在事件的部分.请注意,表单外的控件不能成为成功的控制者.

The elements used to create controls generally appear inside a FORM element, but may also appear outside of a FORM element declaration when they are used to build user interfaces. This is discussed in the section on intrinsic events. Note that controls outside a form cannot be successful controls.

这是我的大胆

提交按钮被视为控件.

http://www.w3.org/TR/html4/interact/forms.html#h-17.2.1

来自评论

我有一个多选项卡式设置区域,其中有一个按钮可以更新所有内容,因为根据它的设计,按钮将在表单之外.

I have a multi tabbed settings area with a button to update all, due to the design of it the button would be outside of the form.

为什么不将 input 放在表单中,而是使用 CSS 将其放置在页面的其他位置?

Why not place the input inside the form, but use CSS to position it elsewhere on the page?

相关文章