【问题标题】:Submit Form in Bootstrap Modal with IronRouter / MeteorJS使用 IronRouter / MeteorJS 在引导模式中提交表单
【发布时间】:2015-08-21 15:03:28
【问题描述】:

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

当前部署版本的链接是: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);
    }
});

【问题讨论】:

  • 我不熟悉流星,所以我不明白它是如何连接事件的 - submitApp 只是一个按钮值,通常你会在表单本身上添加一个事件监听器。为了便于访问,我建议您将按钮放在表单中,以便输入键用于提交等
  • @DominicTobias 啊!接得好。谢谢你。这有助于解决一个难题......现在模式在提交时清除,但数据仍然没有正确注入。

标签: javascript forms twitter-bootstrap meteor iron-router


【解决方案1】:

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

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

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

【讨论】:

  • 在表单元素本身上监听可能会更好,这样返回键提交也会被捕获
猜你喜欢
  • 2014-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-03
  • 2014-07-09
  • 1970-01-01
相关资源
最近更新 更多