【问题标题】:Why this submit function in Meteorjs doesn't work?为什么 Meteorjs 中的这个提交功能不起作用?
【发布时间】:2015-05-21 21:10:52
【问题描述】:

我有一个包含 7 个问题的表格。我使用 display:none 单独询问他们。但问题是提交功能不起作用。当我单击它时,它只是重定向回第一个问题,并且 url 变成 - http://localhost:3000/?problem=&why1=&why2=&why3=&why4=&why5=&solution=&submit-all=Answer 。我真的需要帮助。下面是 HTML 模板和 JavaScript 提交函数的代码提交到 Problems 集合中。

<template name="submitProblem">
  <div class="container">
    <div class="main-page">
      <form class="text-center">
        <div id="submit-problem">
          <input autofocus="autofocus" type="text" name="problem" id="problem" placeholder="What's the problem ?"/>
          <input type="button" id="route" value="Find Its Route" class="route btn btn-sample">
        </div>

         ...
        submit-why1 - submit-why4
         ...

        <div id="submit-why5" class="hidden">
          <input autofocus="autofocus" type="text" id="why5" class="" name="why5" placeholder="This problem exists, because..."/>
          <input type="button" value="Answer" class="btn-success btn answer5">
          <button class="btn back5 back-btn"><i class="fa fa-arrow-left fa-3x"></i></button>
        </div>

        <div id="submit-solution" class="hidden">
          <input autofocus="autofocus" type="text" name="solution" id="solution" placeholder="What could be the best solution ?"/>
            
          <input type="submit" id="submit-all" name="submit-all" value="Answer" class="btn btn-primary submit-all">

          <button class="btn back6 back-btn"><i class="fa fa-arrow-left fa-3x"></i></button>
        </div>
      </form>
    </div>
  </div>
</template>

Template.submitProblem.events({
  'submit .submit-all':function() {
    event.preventDefault();
    var problem = $(event.target).find('[name=problem]').val();
    var why1 = $(event.target).find('[name=why1]').val();
    var why2 = $(event.target).find('[name=why2]').val();
    var why3 = $(event.target).find('[name=why3]').val();
    var why4 = $(event.target).find('[name=why4]').val();
    var why5 = $(event.target).find('[name=why5]').val();
    var solution = $(event.target).find('[name=solution]').val();

   Problems.insert({
      problem: problem,
      why1: why1,
      why2: why2,
      why3: why3,
      why4: why4,
      why5: why5,
      solution: solution,
      submitdate: new Date()
    });

    console.log(problem + why1 + why2 + why3 + why4 + why5 + solution);

    Router.go('submitted');
  }
});

【问题讨论】:

    标签: javascript jquery html forms meteor


    【解决方案1】:

    您需要将event 作为第一个参数传递给您的处理程序:

    submit: function(event) {
      event.preventDefault();
      ...
    

    否则它不会被定义,默认操作(POST)不会被阻止,你的页面会重新加载。

    【讨论】:

    • 谢谢,实际上没有它也可以,但我刚刚意识到我必须提交一个表单 - '提交表单': function(event) 而不是我在上面的代码中所做的按钮。跨度>
    • 是的,好点。我将解决方案修改为只使用submit,因为这里的form 是多余的。
    猜你喜欢
    • 1970-01-01
    • 2018-11-04
    • 1970-01-01
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    • 2021-04-30
    相关资源
    最近更新 更多