Bootstrap模态表单在使用knockout的submit绑定时无法提交。

21 浏览
0 Comments

Bootstrap模态表单在使用knockout的submit绑定时无法提交。

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

我正在一个Bootstrap模态框内构建一个表单。问题是,除非用户点击提交按钮,否则knockout的提交绑定不会执行。我怀疑是Bootstrap阻止了knockout的绑定执行,但我不知道该如何解决这个问题。

如果你按下回车键,模态对话框会关闭,并且绑定函数不会执行(结果无法发送数据)。但是,如果你点击提交按钮,绑定会按预期执行。

以下是相关的代码:

脚本:

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:

        

分享

分享这篇文章

分享

您想将文章发送给谁?

0
0 Comments

问题的原因是表单中的第一个按钮始终是默认按钮,因此在按下回车键时会点击取消按钮。

解决方法有以下几种:

1. 将取消按钮改为标签。

2. 添加type="button"属性。

参考链接:[Multiple submit buttons on HTML form – designate one button as default](https://stackoverflow.com/questions/1963245)

0