我正在使用iron-form
测试自定义元素。我遇到的问题是当我按回车键时表单没有提交。例如,此表格适用于输入:
<iron-form>
<form id="loginForm">
<paper-card heading="Welcome Back!" elevation="1" animated-shadow="true">
<div class="card-content">
<input type="text" label="Username" value="{{username}}">
<input type="text" label="Password" value="{{password}}">
</div>
<div class="card-actions">
<button disabled$="[[fetching]]" type="submit">Login</button>
</div>
</paper-card>
</form>
</iron-form>
我的connectedCallback方法中有一个事件监听器,用于监听iron-form-presubmit event
connectedCallback() {
super.connectedCallback();
this.addEventListener('iron-form-presubmit', function(event) {
event.preventDefault();
alert('working')
});
}
问题是当我将html输入元素切换到纸张输入元素时,表单不再在输入时提交。
<iron-form>
<form id="loginForm">
<paper-card heading="Welcome Back!" elevation="1" animated-shadow="true">
<div class="card-content">
<paper-input type="text" label="Username" value="{{username}}"></paper-input>
<paper-input type="text" label="Password" value="{{password}}"></paper-input>
</div>
<div class="card-actions">
<button disabled$="[[fetching]]" type="submit">Login</button>
</div>
</paper-card>
</form>
</iron-form>
答案 0 :(得分:2)
当按下[enter]
键时,您可以使用<iron-a11y-keys>
来调用提交方法。
<iron-a11y-keys target="loginForm" keys="enter" on-keys-pressed="submit"></iron-a11y-keys>
<iron-form id="loginForm">
...
</iron-form>
在自定义元素定义中,添加方法:
submit: function () {
this.$.loginForm.submit();
}