【问题标题】:Bootstrap modal form won't submit with knockout's submit bindingBootstrap 模态表单不会通过淘汰赛的提交绑定提交
【发布时间】:2013-08-19 01:26:38
【问题描述】:

我在这里有一个 jsFiddle 示例:http://jsfiddle.net/vsZhg/

我正在我的引导模式中构建一个表单。问题是除非用户单击提交输入,否则永远不会执行淘汰提交绑定。我怀疑引导程序正在阻止执行淘汰绑定,但是我不确定如何解决这个问题。

如果您按下回车键,模式对话框将关闭,并且绑定功能永远不会执行(因此我无法发送数据)。但是,如果您点击提交输入,则绑定会按预期执行。

下面是相关代码:

脚本:

function ArticleViewModel() {
    var self = this;

    self.SendArticleName = ko.observable('');
    self.SendArticleEmail = ko.observable('');

    self.SendArticle = function() {
        $.ajax('http://example.com', {
            data: ko.toJSON({ name: self.SendArticleName, email: self.SendArticleEmail }),
            type: "post", contentType: "application/json",
            success: function(result) {
                $('#share-modal').modal('hide');
            }
        });
    };

}

var articleViewModel = new ArticleViewModel();

ko.applyBindings(articleViewModel);

HTML:

<div id="share" class="row article-section">
    <div class="span12">
        <h4>Share</h4>
        <a class="btn btn-large" role="button" data-toggle="modal" href="#share-modal"><i class="icon-envelope"></i> Share This Article</a>

        <div id="share-modal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <form class="form-horizontal" data-bind="submit: SendArticle" class="modal-form">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h3 id="myModalLabel">Share</h3>
                </div>
                <div class="modal-body">
                    <p>Who would you like to send the article to?</p>
                    <br />
                        <div class="control-group">
                            <label class="control-label">Name</label>
                            <div class="controls">
                                <input type="text" placeholder="Name" data-bind="value: SendArticleName" />
                            </div>
                        </div>

                        <div class="control-group">
                            <label class="control-label">Email</label>
                            <div class="controls">
                                <input type="text" placeholder="Email" data-bind="value: SendArticleEmail" />
                            </div>
                        </div>
                </div>
                <div class="modal-footer">
                    <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
                    <input type="submit" value="Send" />
                </div>
            </form>
        </div>
    </div>
</div>

【问题讨论】:

    标签: javascript twitter-bootstrap mvvm knockout.js


    【解决方案1】:

    原因是表单中的第一个按钮始终是默认按钮,因此当您点击Enter 时,您的取消按钮正在被点击。

    这个 SO question 提出了一些解决方法: Multiple submit buttons on HTML form – designate one button as default

    【讨论】:

    • 啊哈!你是绝对正确的。我以前处理过这个问题,但是,它完全让我忘记了。我很快就会将此标记为答案。我将把我的取消按钮切换到&lt;a&gt; 来解决这个问题。谢谢!
    • 你可以添加 type="button" 来修复它
    猜你喜欢
    • 1970-01-01
    • 2013-05-28
    • 2012-05-18
    • 2012-05-30
    • 1970-01-01
    • 1970-01-01
    • 2016-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多