javascript - 防止在 meteor 中单击表单按钮时重新加载页面?

标签 javascript html http meteor

不确定这是 Meteor 问题、JavaScript 问题还是其他问题。单击表单按钮会导致不需要的页面重新加载。

其他信息:

  • 使用 Bootstrap 包
  • 使用 jQuery 包
  • 使用主干包
  • 即使删除了上述软件包,页面重新加载问题仍然存在
  • 注释掉 Posts.insert() 行也不能解决问题

    //来自 application.js
    //*这是应用程序中唯一与事件相关的代码(除了 meteor 从我们这里抽象出来的任何幕后内容)

    Template.new_post.events = { 
    
        'click #submit' : function () {
    
          var text = $('#title').val();
          var cat = $('#category').val();
    
          // save our post with the value of the textbox
          Posts.insert({title : text, category : cat});
        }
    };
    

    //来自 index.html

    <template name="new_post">
      <form class="form-horizontal">
        <fieldset>
        <div class="control-group">
          <!-- Text input-->
          <label class="control-label" for="title">Title</label>
          <div class="controls">
            <input type="text" id="title" value="{{text}}" class="input-xlarge">
            <p class="help-block">Hint: Summarize your post in a few words</p>
          </div>
        </div>
        <div id="form-part-2">
          <div class="control-group">
            <label class="control-label" for="categories">Category</label>
            <div class="controls">
              <select class="input-xlarge" id="category">
                {{#each categories}}
                  <option value="{{defaultLabel}}">{{defaultLabel}}</option>
                {{/each}}
              </select>
              <p class="help-block">Hint: Choose a category</p>
            </div>
          </div>
            <!-- Button -->
            <div class="control-group">
              <div class="controls">
                <button class="btn btn-success" id="submit">Done</button>
              </div>
            </div>
        </div><!-- end div form-part-2 -->
        </fieldset>
      </form>
    </template>
    

最佳答案

我认为您必须在函数结束时返回 false 以防止提交。

关于javascript - 防止在 meteor 中单击表单按钮时重新加载页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13736415/

相关文章:

javascript - 在 MS Lync 中开始与多个用户对话

iphone - 除了 UIWebView 之外,Cocoa 中还有 HTML 包装器吗?

javascript - 如何动态更改输入数字的值并验证其中的数据?

javascript - 如何为动态内容触发 jQuery 单击事件(模态)

javascript - 谷歌地图 API 缩放后形状重复出现问题

javascript - iframe 中<span> 中的innerHTML 不会更改html

c# - selenium c# - 如何获取 div 下的 span 标签文本

http - 422 状态代码是否适用于无法删除对象?

http.ListenAndServe 仅适用于本地主机?

http - Play 忽略继续 100 个请求