event.target在事件中未定义 [英] event.target is undefined in events

查看:761
本文介绍了event.target在事件中未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在事件中使用每个输入值?希望我的下面代码能很好地解释你。

How can one use each input values in events? Hope my below code will explain you well.

HTML:

<template name="UpdateAge">
    {{#each Name}}
    <div data-action="showPrompt">
       <div>
          Some content
       </div>

       <div>
         <div>
            Some content
         </div>
       </div>
       <div>
           Name : {{name}}
           Age : <input type="text" name="age" value="{{age}}"/> //I need to access age values in event
       </div>
    </div>
{{/each}}
<template>

JS:

Template.UpdateAge.events({
  'click [data-action="showPrompt"]': function (event, template) {
        console.log(event.target.age.value); // TypeError: event.target.age.value is undefined
  }
});

我不知道我的方法是否适合将参数值传递给事件欢迎提出建议。

I dont know whether my approach is good for passing parameter values to events so suggestions are welcome.

推荐答案

您使用的符号仅对表单元素和事件有效.target是表单元素。

the notation you are using is valid only for form elements and when event.target IS a form element.

event.target.age.value

event.target 是发生事件的元素, event.currentTarget 是附加了该事件的元素事件处理程序

event.target is the element where the event occurred, event.currentTarget is the element which has attached the event handler

如果用表单替换 div 并且用 currentTarget 替换目标然后它将起作用

if you replace div with form and replace target with currentTarget then it will work

event.currentTarget.age.value

这里是小提琴使用你的代码

这篇关于event.target在事件中未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆