0

关于这里已经提出的问题

但这个问题一直没有得到回答。

我正在尝试在铁形式上添加EventListener,例如:

我的 HTML 导入:

<link rel="import" href="/bower_components/polymer/polymer.html">
<link rel="import" href="/bower_components/iron-ajax/iron-ajax.html">

<link rel="import" href="/bower_components/iron-form/iron-form.html">
<link rel="import" href="/bower_components/paper-input/paper-input.html">
<link rel="import" href="/bower_components/paper-button/paper-button.html">
<link rel="import" href="/bower_components/paper-spinner/paper-spinner.html">

表格:

<form is="iron-form" method="get" action="/" id="eventsDemo">
        <paper-input name="name" label="Name" required auto-validate></paper-input>
        <paper-input name="password" label="Password" type="password" required auto-validate></paper-input>
        <paper-checkbox name="read" required>You must check this box</paper-checkbox>
        <br>
        <paper-button raised onclick="_delayedSubmit(event)" disabled id="eventsDemoSubmit">
            <paper-spinner id="spinner" hidden></paper-spinner>
            Submit
        </paper-button>
        <paper-button raised onclick="_reset(event)">Reset</paper-button>
        <div class="output"></div>
    </form>

和听众:

document.getElementById('eventsDemo').addEventListener('iron-form-submit', function (event) {

    });

但这也让我犯了错误-

未捕获的类型错误:无法读取 null 的属性“addEventListener”

我已经尝试了一些有关 window.onload 或 WebComponentsReady 的建议。没有任何帮助。

4

2 回答 2

0

我找到了解决方案:

我在错误的位置定义了 addEventListener,它需要与 connectedCallback() 中的 shadowRoot 一起使用。

 class UserApp extends Polymer.Element {
        static get is() {
            return 'user-app';
        }

        connectedCallback() {
            super.connectedCallback();

            let eventsDemo = this.shadowRoot.getElementById('eventsDemo');
            let eventsDemoSubmit = this.shadowRoot.getElementById('eventsDemoSubmit');

            eventsDemo.addEventListener('iron-form-submit', function (event) {
                console.log("iron form submit");

                eventsDemoSubmit.disabled = false;
                this.querySelector('.output').innerHTML = JSON.stringify(event.detail);
            });

            eventsDemo.addEventListener('change', function (event) {

                eventsDemoSubmit.disabled = !eventsDemo.validate;
            });
        }
于 2017-06-17T19:58:18.817 回答
0

也许您缺少 } 或 bower 安装

于 2017-06-16T19:59:42.257 回答