铁形式与聚合物2.0

时间:2017-03-28 16:35:47

标签: javascript polymer web-component iron-form

我有一个错误,我在jsbin中复制了这个错误:https://jsbin.com/micinalacu/1/edit?html,console,output

铁形式,当提交序列化方法返回时总是未定义,并且它被调用两次。

 <dom-module id="my-form">
  <template>

    <iron-form id="myForm">
      <form method="get" action="cenfdsas">
        <input type="text" name="cenas">
        <button on-click="cenas">Submit</button>
      </form>
    </iron-form>

  </template>

  <script>
   class MyForm extends Polymer.Element {

      static get is() {
        return 'my-form';
      }

      connectedCallback() {
        super.connectedCallback();
        const form = this.$.myForm;
        form.addEventListener('iron-form-presubmit', function (event) {
          event.preventDefault();
          console.log("here")
          console.log(form.serialize());
        });
      }

      cenas() {
        this.$.myForm.submit();
      }

    }

    window.customElements.define(MyForm.is, MyForm);

  </script>
</dom-module>

更新

Polymer团队需要将方法的名称更改为serializeForm,因为它们有一个错误。资料来源:https://github.com/PolymerElements/iron-form/issues/174

但我继续讨论提交事件它被调用两次的问题 错误 - &gt; https://jsbin.com/koyelafeze/1/edit?html,console,output

1 个答案:

答案 0 :(得分:2)

根据the documentation,表单中使用标准<button>元素会自动提交。

然后,您应该按照链接中的建议使用<paper-button>,或者评论cenas()方法的内容。

JS Bin example