1

I want to use template.find (in Templates.xxx.events) to get the value of the checked radiobutton of a radiobutton group. In jquery I would use $('input:radio[name=XXXXX]:checked').val(). That does not work with template.find. template.find('input:radio[name=XXXXX]:checked')returns null.

What should I be using as CSS selector for this task?

4

1 回答 1

8

您的选择器看起来正确,所以它可能就是您调用它的方式。也许这个例子会有所帮助:

html

<template name="animals">
  <form>
    <input type="radio" name="animal" value="cat">Cat<br>
    <input type="radio" name="animal" value="dog">Dog<br>
    <input type="button" value="Click">
  </form>
</template>

js

Template.animals.events({
  'click :button': function(event, template) {
    var element = template.find('input:radio[name=animal]:checked');
    console.log($(element).val());
  }
});

在这里,我仍然使用 jquery 来提取值,但它演示了选择器的工作原理。

于 2013-06-12T02:36:49.827 回答