gpt4 book ai didi

javascript - Bootstrap 模态表单不会通过 knockout 的提交绑定(bind)提交

转载 作者:行者123 更新时间:2023-11-29 10:46:38 25 4
gpt4 key购买 nike

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

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

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

相关代码如下:

脚本:

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>

最佳答案

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

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

关于javascript - Bootstrap 模态表单不会通过 knockout 的提交绑定(bind)提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18276704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com