Knockout.js / 如何响应绑定下拉列表上的更改事件
应该发生的是页面加载,它调用服务器获取有关订单的信息(此时它只是可能的订单类型的列表和当前订单类型的 ID)。加载后,选择的值应设置为“selected_order_type”。
然而,它看起来只是在列表中选择它,而不是实际将所选项目的“值”设置为当前值。因此,例如在我的示例中,它在加载时选择第二个选项。如果我然后单击第三个选项,那么一切都会正常,如果我首先单击第一个选项,则什么也不会发生,我假设它是因为仍然具有第一个选项值,即使它显示第二个选项作为所选选项。 (对淘汰赛非常陌生,我已经使用它几个小时了,只是想了解它是如何工作的)。
<select id="order_type_select"
data-bind="options:order_types,
optionsText:'order_type',
optionsValue:'order_type_id',
value:selected_order_type"/>
function update_order_field(order_id,field,value){
var url = '/op/update_order_field'
data = {'order_id':order_id,'field':field,'value':value};
console.log(url);
console.log(data);
}
function refresh_order_data(order_id,view_model){
$.getJSON("/op/order_json/"+order_id,function(data){
ko.mapping.fromJS(data,view_model);
});
console.log(view_model.selected_order_type);
}
function OrderData(){
var self = this;
self.order_types = [{}]
self.selected_order_type = '0'
return self
}
$(document).ready(function() {
var order_id = $("#order_id").attr('order_id');
var view_model = ko.mapping.fromJS(new OrderData());
view_model.selected_order_type.subscribe(function(data){
console.log(data);
});
refresh_order_data(order_id,view_model);
ko.applyBindings(view_model);
});
初始加载时从服务器传入的数据...
{
order_types: [
{ order_type_id=1, order_type="Phone"},
{ order_type_id=2, order_type="Fax"},
{order_type_id=3, order_type="Web"}
],
selected_order_type = '2'
}
What should happen is the page loads, it calls the server for information about the order (at this point its just a list of possible order types and the id of the current type of the order). After the load the value of the select should be set to "selected_order_type" which it is.
However it looks like it just selecting it in the list and not actually setting the "value" of the select item to the current value. So for instance in my example it selects the 2nd option on load. If i then click the third option everything works if instead I clicked the first option first nothing happens and I am assuming it because the still had the first options value even though it was showing the second option as the selected option. (very new to knockout been using it for a couple hours just trying to get my head around how it works).
<select id="order_type_select"
data-bind="options:order_types,
optionsText:'order_type',
optionsValue:'order_type_id',
value:selected_order_type"/>
function update_order_field(order_id,field,value){
var url = '/op/update_order_field'
data = {'order_id':order_id,'field':field,'value':value};
console.log(url);
console.log(data);
}
function refresh_order_data(order_id,view_model){
$.getJSON("/op/order_json/"+order_id,function(data){
ko.mapping.fromJS(data,view_model);
});
console.log(view_model.selected_order_type);
}
function OrderData(){
var self = this;
self.order_types = [{}]
self.selected_order_type = '0'
return self
}
$(document).ready(function() {
var order_id = $("#order_id").attr('order_id');
var view_model = ko.mapping.fromJS(new OrderData());
view_model.selected_order_type.subscribe(function(data){
console.log(data);
});
refresh_order_data(order_id,view_model);
ko.applyBindings(view_model);
});
Data coming in from server on initial load ...
{
order_types: [
{ order_type_id=1, order_type="Phone"},
{ order_type_id=2, order_type="Fax"},
{order_type_id=3, order_type="Web"}
],
selected_order_type = '2'
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题出在服务器端。尽管我不知道为什么,因为客户端上的输出是相同的。我使用 python 将返回值包装在 json.dumps 中,一切都按预期工作
Problem was server side. Although I have no idea why as the output on the client is identical. I wrapped the returning value in json.dumps using python and everything works as intended