您與淘汰賽混合jQuery的。提交由jQuery處理。 如果您使用的淘汰賽,讓它來處理提交事件增加了<form>
此submit: submitShortlistForm,
:
<form id="shortlistForm" data-bind="submit: submitShortlistForm, style: { display: application.isShortlisted === false ? 'inline-block' : 'none'}, attr: {action: '@(MVC.GetLocalUrl(MVC.HireOrgJobApplication.ViewApplication(Model.CurrentOrganization.CustomUrl, Model.Job.JobKey, "xxx/ajax-shortlist")))'.replace('xxx', application.applicationKey)}" method="post" style="display:inline;">
@Html.AntiForgeryToken()
<input type="hidden" name="ApplicationKey" data-bind="attr:{ value : application.applicationKey }" />
<button type="submit" class="btn-act jui-tooltip" title="Shortlist">
<i class="fa fa-2x fa-star"></i>
</button>
</form>
然後,在您的視圖模型補充一點:
self.submitShortlistForm = function(){
// I think is better to use $.post() or $.ajax().
// I use the pluging because is in your question.
$('form#shortlistForm').ajaxForm(function() {
// Allways change position [0] of the observable array
self.applications()[0].isShortlisted = true;
var data = self.applications().slice(0);
self.applications([]);
self.applications(data);
});
$(this).ajaxSubmit();
}
在淘汰賽提交事件我將該表單與ajaxForm
插件綁定,然後提交。我不確定jQuery插件,我會使用$.post()
或$.ajax()
。
你可以在這個鏈接The "submit" binding找到更多關於Knockout的信息。
我不喜歡這個解決方案,因爲您要刪除self.applications
可觀察數組的所有內容。最好使用mapping plugin並將所有的application
對象(及其內容)轉換爲可觀察對象。但是用這種方法,你應該修改你的ViewModel和視圖。
更新1(19/06/2017)
如果這<form>
是foreach
循環中,那麼self.submitShortlistForm
可能是這樣的:
self.submitShortlistForm = function(application){
$('form#shortlistForm').ajaxForm(function() {
// Updates the current row element.
application.isShortlisted = true;
var data = self.applications().slice(0);
self.applications([]);
self.applications(data);
});
$(this).ajaxSubmit();
}
和HTML:
<form id="shortlistForm" data-bind="submit: $root.submitShortlistForm, ...">
希望這會有所幫助。
請不要問很多關於同一問題的問題。只詢問一個問題並編輯它以更改其內容。 :-) –
你可以發佈viewmodels'viewModel'和'applicationViewModel'嗎? –
如果'applicationViewModel'在你的ViewModel中是可觀察的,那麼你應該在你的Ajax函數中寫'viewModel.applicationViewModel()。isShortlisted(true);'。即,將'()'添加到'applicationViewModel'。 –