javascript - 使用 IronRouter/MeteorJS 在 Bootstrap Modal 中提交表单

标签 javascript forms twitter-bootstrap meteor iron-router

我正在尝试向我正在创建的类似于食谱的网络应用程序提交新的开胃菜。该布局有一个恒定的标题/导航,允许用户添加新的开胃菜,这会触发一个包含开胃菜名称、描述和照片的模式和表单。我希望模式在提交后关闭,并保留在触发模式的当前页面上。目前,我的提交表单无法正常工作,我不确定问题出在哪里......

当前部署版本的链接是:http://reed-cookbook.meteor.com/

我的路由器:

// lib/router.js

Router.configure({
    layoutTemplate: 'layout'
});

Router.map(function() {
    this.route('allRecipes', {
        path: '/'
    });

    this.route('appetizers', {
        path: '/appetizers'
    });

    this.route('appetizerPage', {
        path: '/appetizers/:_id',
        data: function() { return Appetizers.findOne(this.params._id); }
    });

    this.route('desserts', {
        path: '/desserts'
    });

    this.route('mainCourses', {
        path: '/maincourses'
    });

    this.route('submit', {
        path: '/appetizerSubmit'
    });
});

我的开胃菜 html 表单:

// client/views/forms/appetizerForm.html

<template name="appetizerForm">
    <!-- This is the appetizer modal -->
    <div class="modal fade" id="myAppModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">Add An Appetizer</h4>
          </div>
          <div class="modal-body">
              <!-- This is the appetizer form -->
              <form>
                  <div class="form-group">
                      <label for="inputNewLabel">Name</label>
                      <input type="text" class="form-control" id="addNewAppetizer" name="appName" placeholder="What is the name of this appetizer?">
                  </div>
                  <div class="form-group">
                      <label for="inputNewLabel">Description</label>
                      <input type="text" class="form-control" id="addNewAppDesc" name="appDesc" placeholder="Share details about your appetizer.">
                  </div>
                  <div class="form-group">
                      <label for="inputNewLabel">Add Photo</label>
                      <input type="file" id="addNewAppPic" name="appPic">
                      <p class="help-block">Upload a photo of your appetizer.</p>
                  </div>
              </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="submit" class="btn btn-primary" value="submitApp">Submit Appetizer</button>
          </div>
        </div>

我的 appetizerForm.js 用于此特定的 html 功能:

// client/views/forms/appetizerForm.js

Template.appetizerForm.events({
    'submitApp': function(e) {
        e.preventDefault();

        var appetizer = {
            name: $(e.target).find('[name=appName]').val(),
            description: $(e.target).find('[name=appDesc]'),
            photo: $(e.target).find('[name=appPic]').val()
        }

        appetizer._id = Appetizers.insert(appetizer);
        Router.go('/', appetizer);
    }
});

最佳答案

您的事件映射语法错误,它必须是eventType cssSelector

Template.appetizerForm.events({
  "submit form": function(event, template) {
    event.preventDefault();
    [...]
    template.$(".modal").modal("hide");
  }
});

如果您希望模式在提交表单时关闭,请使用 jQuery 插件语法调用模式上的 hide 方法。

关于javascript - 使用 IronRouter/MeteorJS 在 Bootstrap Modal 中提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30687255/

相关文章:

javascript - 如果选择特定单选按钮如何显示表单

twitter-bootstrap - Twitter Bootstrap 像侧面的垂直导航栏

JavaScript 正则表达式 : Find a tag and get some attribute values

javascript - 将 Base64 数据字符串转换为 PDF 文件,以便从 API - NodeJS 下载

html - Materialise Framework - 所需字段的验证不起作用

javascript - 如何在 LabVIEW 中使用 IWebBrowser2 读取 Web 浏览器的隐藏文档值?

html - 用户不应该能够选择已经选择的按钮

css - css 网格框架是否适合创建类似布局的应用程序?

javascript - 我的 JavaScript 使服务器崩溃

javascript - 使用 Passportjs 刷新页面后保持身份验证