【问题标题】:Get value of button in polymer获取聚合物中按钮的值
【发布时间】:2019-04-07 01:47:59
【问题描述】:

我正在尝试从按钮获取设置值,以便在我的函数中进一步处理它。

<template is="dom-if" if="[[Student.Absence]]">
        <td><button class="defaultbutton" value="[[Student.studentnum]]"></button></td>
        <td><button class="absentbutton" value="[[Student.studentnum]]" on-click="absenceHandler(this.value)"></button></td>
     </template>




 absenceHandler(studID){
    let DummyStudents = this.get(['DummyStudents']);
    console.log(studID);
    alert(DummyStudents[1]);
  }

我希望将值插入到我的函数中,但我得到了这个:

监听方法absenceHandler(this.value)未定义 处理程序@template-stamp.html:98 (匿名)@templatize.html:160

【问题讨论】:

    标签: javascript polymer polymer-2.x


    【解决方案1】:

    不需要显式地将值发送给方法。您可以按如下方式使其工作。

     <td><button class="absentbutton" value="[[Student.studentnum]]" on-click="absenceHandler"></button></td>
    
    
    absenceHandler(e){
    let studID = e.currentTarget.value;
        let DummyStudents = this.get(['DummyStudents']);
        console.log(studID);
        alert(DummyStudents[1]);
      }
    

    【讨论】:

    • 还有; this.Student.studentnum 给出相同的:)
    • 谢谢你做了不死的工作。但是我不完全明白你为什么给函数一个参数?或者它是如何运作的。
    • 该参数将作为调用此函数的事件隐式传递。所以这里是 mouseEvent ,它将包含与按钮及其属性相关的所有数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    相关资源
    最近更新 更多