Bootstrap模态表单不会与knockout的提交绑定一起提交

时间:2013-08-16 15:15:46

标签: javascript twitter-bootstrap mvvm knockout.js

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

我正在我的bootstrap模式中构建一个表单。问题是除非用户点击提交输入,否则永远不会执行敲除提交绑定。我怀疑bootstrap阻止执行敲除绑定,但是,我不知道如何解决这个问题。

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

以下是相关代码:

脚本:

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>

1 个答案:

答案 0 :(得分:3)

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

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

相关问题