如何在不使用流星功能的情况下获取按钮的值?

时间:2015-08-28 05:17:10

标签: meteor

我是Meteor的新手,这就是我在html文件中保留按钮的方式。

<input type="button" class="number" value="1">
<input type="button" class="number" value="2">
<input type="button" class="number" value="3">

如何在js文件中获取这些按钮的值。

任何人帮助我。

提前致谢。

3 个答案:

答案 0 :(得分:0)

确定点击了哪些按钮,您只需使用模板的事件处理程序即可。知道通过类名识别按钮非常重要。因此,您必须选择唯一的类名

例如,如果您在名为template1的模板中有按钮,则只需执行以下操作:

//in your .html inside your template1

<button class="button1">Button1</button>
<button class="button2">Button2</button>
<button class="button3">Button3</button>

和相应的JS:

//in your clientside JS

Template.template1.events({
  "click.button1": function () {
    //exec code when button1 clicked
    //[...]
  },
  "click.button2": function () {
    //exec code when button2 clicked
    //[...]
  },
  "click.button3": function () {
    //exec code when button3 clicked
    //[...]
  }
});

如果您的按钮不在模板中,而只是在您的内部使用该区域作为模板。例如 Template.body.events 将处理您身体中的事件。

答案 1 :(得分:0)

使用事件获取值:

'click .number': function(event, template) {
    console.log(event.currentTarget.value);
}

或普通的Jquery:

$($('.number')[0]).val()
$($('.number')[1]).val()
$($('.number')[2]).val()

答案 2 :(得分:0)

可以像下面这样完成:

Template.TemplateName.events({
 'click .number': function (event,template) {
     return event.target.value;
 }
});
相关问题