如何在Meteorjs中提交隐藏输入的值

时间:2016-01-25 01:29:56

标签: javascript jquery forms meteor submit

在我的流星应用程序中,我通过点击下面的sbmit按钮提交选择器号码的值

enter image description here

当我尝试使用Enter键(键盘)提交时,它不起作用,知道我设置了隐藏的输入并尝试提交它;我认为问题来自隐藏的输入,所以经过一个小时的搜索,我不知道如何提交隐藏的输入,请帮助;这是我的代码

<template name="range">
<div class="row">
<div class="col-xs-9 col-sm-9 col-md-9 col-lg-9">
<div class="valueSliders">
  <div class="verticleNumberSelect">
  <span class="firstNumber">
    <span class="up"> </span>
    <span class="numberValue" tabindex="1">0</span>
    <span class="down"></span>
  </span>
  <span class="secondNumber">
    <span class="up"></span>
    <span class="numberValue" tabindex="1">0</span>
    <span class="down"></span>
  </span>
  <span class="thirdNumber">
    <span class="up"></span>
    <span class="numberValue" tabindex="1">0</span>
    <span class="down"></span>
  </span>
  <span class="fourthNumber">
    <span class="up"></span>
    <span class="numberValue" tabindex="1">0</span>
    <span class="down"></span>
  </span>
  <form>
  <input type="hidden" id="verticleNumber" />
  <input type="submit" style="visibility: hidden; position: absolute;" />

  </form>

</div>
</div>
</div>
</template>

我的js是

Template.range.events({
'submit form': function (event) {

    event.preventDefault();

    if (Session.get('times') && Session.get('times') > 0) {
    var RESPONSE, responseClass;
    var GUESS = $('#verticleNumber').val();
    if (GUESS >  ANSWER) {
        RESPONSE = "-";
        responseClass = 'lower';
    }
    else if (GUESS <  ANSWER) {
        RESPONSE = "+";
        responseClass = 'higher';
    }
    else if (GUESS ==  ANSWER) {
        RESPONSE = "✔";
        responseClass = 'win';
      }

     }
    }
});

我动态设置隐藏输入的值,它是玩家用选择器编号选择的值,我的意思是verticleNumber的值;回答它随机值

非常感谢

1 个答案:

答案 0 :(得分:1)

如果表单被认为是焦点,则输入键仅适用于表单 - 我认为只有一个隐藏输入的表单会阻止该表单的发生。假设您的提交函数根本没有被调用,我实际上打破了Meteor约定并在模板的onCreated函数中执行jQuery事件监听器:

$(document).keypress(function(e) {
    if(e.which == 13) {
        console.log('enter pressed');
        // Your code here to get values and do stuff.
    }
});