【问题标题】:Fire a remote bootstrap modal when clicking a form submit button, remote modal content then has form data to be displayed?单击表单提交按钮时触发远程引导模式,远程模式内容然后显示表单数据?
【发布时间】:2014-07-09 02:05:33
【问题描述】:

我很难找到实现此目的的方法。

我正在使用 Node.js、Bootstrap 3 和 express

我想从表单页面开始。当用户填写表单时,他们点击提交。在这里,我想触发一个远程模式。 (通过常规链接或按钮轻松完成此操作。)

这会启动一个帖子。然后如何触发远程模式并将表单数据从帖子传递给它,以便可以在原始表单页面上方的模式窗口中显示带有答案的新远程页面。

这是我的代码:

表格翡翠(cover-form.jade):

html
    head
        meta(name="viewport" content="width=device-width, initial-scale=1.0")
        title= title
        link(href="/stylesheets/coverform_custom.css" rel="stylesheet")
    body
        .modal.fade(id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true")
            .modal-dialog.wide-modal
                .modal-content
        form.form-horizontal(id="coverform" name="coverform" method="post" action="")
            .LoginBox.LoginText
                fieldset.animated.fadeInUp
                    // Form Name
                    legend.LoginText  Cover Strategy
                    // Text input
                    .form-group
                        label.col-xs-4.control-label(for='symbol') Stock Ticker:
                        .col-xs-5
                            input#symbol.form-control.input-xs(type='text', name='symbol', placeholder='example: AAPL', autofocus, required)
                            span.help-block Enter Stock Symbol (all capitals)
                    // Text input
                    // Select Basic
                    .form-group
                        label.col-xs-4.control-label(for='optiondate') Option Date:
                        .col-xs-5
                            select#optiondate.form-control(name='optiondate' required)
                                - for opdate in OptionDates
                                    option(value="#{opdate.OptionDate}") #{moment(opdate.OptionDate).format('dddd, MMMM Do, YYYY')}
                            span.help-block Date the option closes.
                    .form-group
                        label.col-xs-4.control-label(for='strikeprice') Strike Price:
                        .col-xs-5
                            input#strikeprice.form-control.input-xs(type='number', name='strikeprice', placeholder='example: 100.00',  pattern="[0-9]+([\.|,][0-9]+)?", min="0", step="0.50", required)
                            span.help-block Strike price of the option.
                    // Text input
                    .form-group
                        label.col-xs-4.control-label(for='optionprice') Option Selling Price:
                        .col-xs-5
                            input#optionprice.form-control.input-xs(type='number', name='optionprice', placeholder='example: .45', pattern="[0-9]+([\.|,][0-9]+)?", min="0", step="0.05", required)
                            span.help-block Price the option is selling for.
                    // Button
                    .form-group
                        label.col-xs-4.control-label(for='submit')
                        .col-xs-4
                            button#submit.btn.btn-primary(name='submit' type='submit') Submit
        script(src='/javascripts/jquery-2.0.3.min.js')
        script(src='/javascripts/bootstrap.min.js')

Remote Modal Jade(尚未引用任何表单数据) answer.jade:

html
    body
        .modal-header
            button(type="button" class="close" data-dismiss="modal" aria-hidden="true")×
            h4.modal-title(id="myModalLabel") This Is An Awesome Title
        .modal-body 
            p This Body Is Great
        .modal-footer
            button.btn.btn-default(data-dismiss="modal") Close

用于路由的 app.js:

...

app.get('/', routes.index);
app.get('/cover', cover.coverForm);
app.post('/cover', cover.doCover);

...

cover.js:

exports.doCover = function(req, res) {
    var mystockdata = [];
    console.log("Here is the symbol I got:    " + req.body.symbol);
    StockPrices.find({
        Symbol: req.body.symbol
    })
        .sort('Date')
        .exec(function(err, stockdata) {
            if (!err) {
                stockdata.forEach(function(stockdata) {
                    mystockdata.push({
                        "MarketDate": stockdata.Date,
                        "Open": stockdata.Open,
                        "High": stockdata.High,
                        "Close": stockdata.Close,
                        "Volume": stockdata.Volume
                    });
                });
                console.log("Here is the stock Data:    " + stockdata);
            } else {
                console.log(err);
            }
            res.render('answer', {});
        });
};

所以我的问题真的是我如何路由这个,以便在点击 cover-form.jade 上的提交时我可以执行引导模式并使用 doCover 函数触发渲染模式内容的帖子?

也许我也完全错误地处理了这个问题,不知道如何完成我想要的。

非常感谢任何帮助。

【问题讨论】:

    标签: html node.js express twitter-bootstrap-3 pug


    【解决方案1】:

    能够使其工作,但不能远程工作

    默认情况下,Bootstrap 3 按钮是提交类型,所以我使用了一个触发模式的标准按钮...

    button#submit.btn.btn-primary(name='submit' data-toggle="modal" data-target="#myModal") Submit
    

    然后我将标准 Bootstrap 模态的东西放在我的页面顶部:

    .modal.fade(id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true")
      .modal-dialog.wide-modal
        .modal-content
          .modal-header
            button.close(type='button', data-dismiss='modal', aria-hidden='true') ×
            h4.myModalLabel.modal-title 
            | #{title}
          .modal-body
              #container(style="width:90%; height:400px;")
          .modal-footer
            button.btn.btn-primary(type='button', data-dismiss='modal') Close
    

    然后真正的技巧是检查从 app.js 触发的路由是 post 还是 get。如果它是表示表单已提交的帖子,那么我使用 jQuery 来显示模式。否则它会尝试显示,但会立即返回到表单。我通过检查仅在 post 函数 (strikeprices) 上呈现的变量来做到这一点:

    script.
      $(document).ready(function(){
    
        if(#{strikeprices}){
          $('#myModal').modal('show')
        }
    
        $('body').on('hidden.bs.modal', '.modal', function () {
          $(this).removeData('bs.modal');
        });
      });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-08
      • 1970-01-01
      相关资源
      最近更新 更多