聚合物:手动提交表格 [英] Polymer: manually submitting a form
问题描述
在聚合物中,我尝试手动提交表单.我的表单如下:
In polymer I'm trying to manually submit a form. My form looks like this:
<form id="myForm" on-submit="{{ submitForm }}">
<input class="text" value="{{ someValue}}">
<button type="submit">Submit</button>
</form>
在聚合物对象中,我有:
And in the polymer object I have:
submitForm: function(e) {
e.preventDefault();
}
每当我尝试执行以下操作时:
Whenever I try to do the following:
document.getElementById('myForm').submit();
表单完全忽略了on-submit属性,并将表单发布到新页面.
the form totally ignores the on-submit attribute and posts the form to a new page.
我正在为任何想知道为什么要这样做的人构建一个屏幕键盘.每当有人按下屏幕键盘上的Enter键时,我都需要提交表单.
I'm building a on-screen keyboard for anyone wondering why I would want to do this. I need to submit the form whenever someone hits the enter key on the on-screen keyboard.
有人知道为什么会这样吗?
Does anyone know why this happens?
一个JSBin示例向您显示确切的问题(请参阅警报): http://jsbin.com /wadihija/2/
A JSBin example to show you the exact problem (see the alerts): http://jsbin.com/wadihija/2/
推荐答案
来自 MDN页面关于submit
:
调用此表单时,不会触发表单的onsubmit事件处理程序 方法...不能保证可以由HTML用户代理调用.
The form's onsubmit event handler will not be triggered when invoking this method ... it is not guaranteed to be invoked by HTML user agents.
但是,在submit
类型的按钮上调用click
似乎可行.看到这里:
However, calling click
on a submit
type button seems to work. See here:
这是对您的jsbin的修改,我相信它可以满足您的要求:
Here is a modification of your jsbin that I believe does what you want:
http://jsbin.com/wadihija/6/edit
这篇关于聚合物:手动提交表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!