使用jQuery与流星给出错误

时间:2014-02-19 10:15:58

标签: javascript jquery meteor

省略keydown input : function(event) {}会给我一个错误 “While building the application: client/client.js:33:11: Unexpected token (”  这基本上就是首发。我想知道为什么我一开始就需要javascript函数。没有得到错误。这是一个问题,特别是因为我不想每次按下键时都运行click功能。在任何情况下,要么弄清楚如何在这里使用jQuery而不是javascript或更改keydown输入会很棒。

Template.create_poll.events = {

  'keydown input' : function(event) {

    $("input").keypress(function() {
      var active_element = $(this).parent().attr("id");
      var last_child = $('ul li:last').attr("id");
      var option_number_index = last_child.lastIndexOf("-");
      var option_number = last_child.substring(option_number_index+1);
      option_number = option_number/1;

        //console.log(option_number);

        //console.log(last_child);
      if (last_child == active_element) {
        console.log(active_element);
        option_number += 1;
        console.log(option_number);
        $('ul').append('<li id="poll-choice-' +  option_number + '"><input name="choice" type="text" placeholder="Option ' + option_number + '">');
      }
    }); 


    $("#poll_create").click(function() {
      console.log("Button works");
      var choices = new Array();
      var counter = 0;
      $("ul li input").each(function() {
        choices[counter] = $(this).val();
        counter++;
      });

      console.log(choices[1]);
      console.log(choices[5]);

    });

  }



}

1 个答案:

答案 0 :(得分:1)

Template.create_poll.events期望eventMap为:

  

事件映射是一个对象,其中属性指定要处理的一组事件,值是这些事件的处理程序。该属性可以采用以下几种形式之一:

因此,您需要传入'keydown input' : function (event, templ) { ... }以使其成为有效的Javascript对象。

在这种情况下,您应该遵循@ Cuberto的建议并使用Meteor的事件地图实现事件:

Template.create_poll.events = {

  'press input' : function(event) { 
      var active_element = $(this).parent().attr("id");
      var last_child = $('ul li:last').attr("id");
      var option_number_index = last_child.lastIndexOf("-");
      var option_number = last_child.substring(option_number_index+1);
      option_number = option_number/1;

        //console.log(option_number);

        //console.log(last_child);
      if (last_child == active_element) {
        console.log(active_element);
        option_number += 1;
        console.log(option_number);
        $('ul').append('<li id="poll-choice-' +  option_number + '"><input name="choice" type="text" placeholder="Option ' + option_number + '">');
      }
  },
  'click #poll_create' : function (event) {
      console.log("Button works");
      var choices = new Array();
      var counter = 0;
      $("ul li input").each(function() {
        choices[counter] = $(this).val();
        counter++;
      });

      console.log(choices[1]);
      console.log(choices[5]);

    }
}

但是,如果您想使用某些特定于jQuery的事件,那么您可以将它们附加到rendered函数中:

Template.create_poll.rendered = function () {
     $("input").keypress(function() {
      var active_element = $(this).parent().attr("id");
      var last_child = $('ul li:last').attr("id");
      var option_number_index = last_child.lastIndexOf("-");
      var option_number = last_child.substring(option_number_index+1);
      option_number = option_number/1;

        //console.log(option_number);

        //console.log(last_child);
      if (last_child == active_element) {
        console.log(active_element);
        option_number += 1;
        console.log(option_number);
        $('ul').append('<li id="poll-choice-' +  option_number + '"><input name="choice" type="text" placeholder="Option ' + option_number + '">');
      }
    }); 


    $("#poll_create").click(function() {
      console.log("Button works");
      var choices = new Array();
      var counter = 0;
      $("ul li input").each(function() {
        choices[counter] = $(this).val();
        counter++;
      });

      console.log(choices[1]);
      console.log(choices[5]);

    });
};