由於某些原因,當我使用data-bind =「with:detailedStudent」時,jQuery change()綁定不會被調用。我動態地填充選擇選項,但我不確定這應該重要。這是一些我使用的只是儘量給怎麼回事的體面圖片代碼:knockout.js data-bind'with'與jQuery change事件衝突
var viewModel;
$(document).ready(function() {
viewModel = new StudentViewModel();
ko.applyBindings(viewModel);
// this change event is not getting called, but if i put the onchange directly into the html as an attribute, it works fine.
$("#accountDialog").find("#mySelect").change(function() {
alert('hi');
}
}
function Student(data) {
var self = this;
ko.mapping.fromJS(data, {}, this);
}
function StudentViewModel() {
var self = this;
this.students = ko.observableArray([]);
this.detailedStudent = ko.observable();
}
<div id="accountDialog" class="modal hide fade" data-bind="with: detailedStudent">
<select id="mySelect" name="mySelect" data-bind="value: GraduationClassId"></select>
</div>
是的,這確實是......這是我補充的內容:this.GraduationClass.GraduationClassId.subscribe(函數(){ 警報( '你好'); }); –