简体   繁体   English

Knockout.js:从使用ajax调用填充的select中获取选项文本

[英]Knockout.js: Get options text from select populated with ajax call

I'm working with a web app using knockout.js as front-end data manipulator, and fetching de data from the server with AJAX calls. 我正在使用一个使用敲门声(Knockout.js)作为前端数据操纵器的Web应用程序,并使用AJAX调用从服务器获取数据。

I have a series of drop-downs which are populated via AJAX, using id as values and strings as optionText, and I need to catch, in different divs, the optionText of the selected option of these drop-downs. 我有一系列的下拉列表,这些下拉列表是通过AJAX填充的,使用id作为值,使用字符串作为optionText,并且我需要在不同的div中捕获这些下拉列表中所选选项的optionText。 So far, using different methods from other Stack Overflow answers, I've not been able to do it. 到目前为止,使用与其他Stack Overflow答案不同的方法,我还没有做到这一点。

Here is the code: 这是代码:

View 视图

<div>
     <select data-bind="options: provinces,optionsText:'name',optionsValue:'id',value: provincesSelected "></select>
        <select data-bind="options: regions,optionsText:'name',optionsValue:'id',value: regionsSelected"></select>
     <select data-bind="options: cities,optionsText:'name',optionsValue:'id',value:citiesSelected"></select>
</div> 
<div>
    <div>
        <div>
            <div>
                <span data-bind="text: citiesSelected().name"></span>
            </div>     
        </div>
    </div>     
</div>

Viewmodel 视图模型

function getProvinces(provinces) {
$.ajax({
    url: "resturl1",
    type: "POST",
    success: function (data) {
        var dats = JSON.parse(data);

        provinces(dats);
        return provinces;

    }
});}function getRegionsByProvince(regions, province) {
$.ajax({
    url: "resturl2",
    type: "POST",
    data: {province: province},
    success: function (data) {
        var dats = JSON.parse(data);

        regions(dats);

        return regions;

    }
});}function getCitiesByRegion(cities, region) {
$.ajax({
    url: "resturl3",
    type: "POST",
    data: {region: region},
    success: function (data) {
        var dats = JSON.parse(data);

        cities(dats);

        return cities;

    }
});}

function AppViewModel() {
var self = this;

self.provinces = ko.observableArray();
self.provincesSelected = ko.observable();
self.regions = ko.observableArray([]);
self.regionsSelected = ko.observable();
self.cities = ko.observableArray([]);
self.citiesSelected = ko.observable();

self.provinces(getProvinces(self.provinces));

self.provincesSelected.subscribe(function (val) {
    self.regions(getRegionsByProvince(self.regions, val));
});
self.regionsSelected.subscribe(function (val) {
    self.cities(getCitiesByRegion(self.cities, val));
});


}ko.applyBindings(new AppViewModel());

This is the last thing I tried (get a "name" property that comes in the array of objects from the server response), but the console throws the error: 这是我尝试的最后一件事(从服务器响应中获取对象数组中的“名称”属性),但是控制台抛出错误:

Uncaught TypeError: Unable to process binding "text: function (){return citiesSelected().name }" Message: Cannot read property 'name' of undefined 未捕获的TypeError:无法处理绑定“文本:function(){return citySelected()。name}”消息:无法读取未定义的属性“ name”

Not using the property in the div data-bind shows the value, but I need the text. 不使用div数据绑定中的属性显示值,但是我需要文本。

I'm quite new at Knockout, so maybe I'm making a huge newbie mistake, but I would apreciate a lot your help. 我在淘汰赛上还很新,所以也许我犯了一个新手错误,但是我会很感谢您的帮助。

If you remove the below, it works: 如果删除以下内容,它将起作用:

optionsValue:'id'

When you set the optionsValue attribute, you are telling Knockout to set selectedCities equal to the id of the selected city. 设置optionsValue属性时,您要告诉淘汰赛将selectedCities设置为等于所选城市的ID。 That is why you got the error stating that the name property doesn't exist. 这就是为什么您会收到错误消息,指出name属性不存在的原因。 You were essentially trying to bind to selectedCities.id.name which definitely doesn't exist. 您实质上是在尝试绑定到绝对不存在的selectedCities.id.name。 By removing the optionsValue attribute, you will be capturing the entire selected city object, and the binding will evaluate the name property as expected. 通过删除optionsValue属性,您将捕获整个选定的城市对象,并且绑定将按预期评估name属性。

Working fiddle: https://jsfiddle.net/dw1284/dqzb3zwn/1/ 工作提琴: https : //jsfiddle.net/dw1284/dqzb3zwn/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM