2015-03-18 93 views
0

我有一個由7個問題組成的表單。我用顯示器分別問他們:沒有。但問題是提交功能不起作用。當我點擊它時,它只是重定向回到第一個問題,網址變成 - http://localhost:3000/?problem=&why1=&why2=&why3=&why4=&why5=&solution=&submit-all=Answer。我真的需要這方面的幫助。以下是HTML模板和JavaScript提交函數在Problems集合中提交的代碼。爲什麼在Meteorjs中這個提交函數不起作用?

<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'); 
 
    } 
 
});

回答

2

您需要將event傳遞作爲第一個參數到您的處理:

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

否則將不會被定義,默認操作(POST)將不會被阻止,並且您的頁面將被重新加載。

+0

謝謝,實際上它沒有它,但我只是意識到我必須提交一個表單 - '提交表單':函數(事件)不是像我這樣做的按鈕,而是在上面的代碼中。 – mhlavacka 2015-03-18 21:36:58

+0

是的好點。我修改瞭解決方案,只是使用'submit',因爲'form'在這裏是多餘的。 – 2015-03-18 22:06:52

相關問題