流星单选按钮不会返回正确的值

时间:2014-02-19 23:18:19

标签: javascript html meteor

当我在Meteor中有一个带有两个单选按钮的表单时,第一个值总是在.val()调用中返回。访问哪个单选按钮的正确方法是什么?


以下是我的示例代码:

testRadioButtons.html:

<template name="testRadioButtons">
    <form class="main">
        <div class="control-group">
            <div class="controls">
                <input name="insuranceDecision" type="radio" value="NoInsurance"/> &nbsp Don't Buy Insurance<br><br>
                <input name="insuranceDecision" type="radio" value="BuyInsurance"/>  &nbsp Buy Insurance<br><br>
            </div>
        </div>
        <div class="control-group">
            <div class="controls">
                <input type="submit" value="Submit" class="btn btn-primary"/>
            </div>
        </div>
    </form>
</template>

testRadioButtons.js

  Template.testRadioButtons.events({
      'submit form': function(e) {
        e.preventDefault();

        console.log('running form submit')

        decision = $(e.target).find('[name=insuranceDecision]').val();
        // console.log($(e.target).find('[name=insuranceDecision]'));
        console.log("decision: " + decision); // ZZZZZ Why is NoInsurance always displaying, even when the other radio button is checked.?

    Meteor.Router.to('happyPage');
  }
});

以下是需要我写的东西,但没有做任何重要的事情。 main.html中

<head>
  <title>testRadioButtons</title>
</head>

<body>
{{renderPage}}
</body>

router.js

Meteor.Router.add({
    '/': 'testRadioButtons',
    '/happyPage' : 'happyPage'
})

happyPage.html

<template name="happyPage">
    <p> Happy page </p>
    <p><a href={{testRadioButtonsPath}}>Test Radio Buttons Again</a></p>
</template>

1 个答案:

答案 0 :(得分:0)

我认为这就是答案:

https://stackoverflow.com/a/4196937/233382

我必须遍历各个元素并找到“已检查”的元素。