使用敲除js获取json对象的选项标题

使用敲除js获取json对象的选项标题,json,knockout.js,Json,Knockout.js,在这里,我想使用knockout js获取json对象的选项文本 html 如何使用下拉标题填充optionText?在列表中添加空值选项: <option value="">- Select -</option> -选择- 更新如下: 这就是你想要达到的目标吗 更新: 我不太清楚您想做什么,但您的意思可能是需要绑定到动态数组? 你能详细说明一下吗?你也可以用更明显的方式来阐述 Html 以下是完整版本:否,我想将选项文本绑定到此处的对象,“Aluth”和“Par

在这里,我想使用knockout js获取json对象的选项文本

html


如何使用下拉标题填充optionText?

在列表中添加空值选项:

<option value="">- Select -</option>
-选择-
更新如下:

这就是你想要达到的目标吗

更新:

我不太清楚您想做什么,但您的意思可能是需要绑定到动态数组?


你能详细说明一下吗?

你也可以用更明显的方式来阐述

Html


以下是完整版本:

否,我想将选项文本绑定到此处的对象,“Aluth”和“Parana”文本应与对象绑定
var ViewModel = function() {
    this.VehicleType = ko.observable();
    this.Mileage = ko.observable();
    this.optionText = ko.observable();

    this.VehicleType.subscribe(function(newValue) {
        if (newValue === "New") {
            this.Mileage(0);
        }
    }, this);
};

ko.applyBindings(new ViewModel());
<option value="">- Select -</option>
<select data-bind="options: vehicleTypes, optionsText: 'Name', value: selectedVehicle">
</select>

<pre data-bind="text: ko.toJSON($root, null, 2)"></pre>
var ViewModel = function() {
    var Vehicle = function(name, type) {
        this.Name = name;
        this.Type = type;
    }; 
    this.vehicleTypes = ko.observableArray([
            new Vehicle("Aluth", "New"),
            new Vehicle("parana", "Used")
        ]);
    this.selectedVehicle = ko.observable();

    this.selectedVehicle.subscribe(function(newValue) {
        alert(newValue.Name);
        alert(newValue.Type);
    }, this);
};

ko.applyBindings(new ViewModel());