Javascript 淘汰js条件选项绑定

Javascript 淘汰js条件选项绑定,javascript,html,knockout.js,Javascript,Html,Knockout.js,在Knockoutjs中,是否可以为选项绑定的子元素设置条件 e、 g: 结帐 您可以这样做: <select data-bind="value: selectedCountry , foreach : countryArray" > <!-- ko if: show --> <option data-bind="value : name, text : name "></option>

在Knockoutjs中,是否可以为选项绑定的子元素设置条件

e、 g:

结帐

您可以这样做:

 <select  data-bind="value: selectedCountry , foreach : countryArray" >
       <!-- ko if: show -->        
            <option data-bind="value : name, text : name "></option>
       <!-- /ko -->
 </select> ​

function viewModel() {

    var self = this;
    this.countryArray = ko.observableArray([{"name" : "France" ,"show" : true},
                                            {"name" : "Italy" , "show" : true},
                                            {"name":"Germany" , "show" : false}]);

    this.selectedCountry = ko.observable("");

}

$(document).ready(function() {

    var vm = new viewModel();
    ko.applyBindings(vm);

})​
 <select  data-bind="value: selectedCountry , options : countryArray, optionsText: 'name', optionsAfterRender: setOptionsShow" ></select>  ​

function viewModel() {

    var self = this;
    this.countryArray = ko.observableArray([{"name" : "France" ,"show" : true},
                                            {"name" : "Italy" , "show" : true},
                                            {"name":"Germany" , "show" : false}]);

    this.selectedCountry = ko.observable("");
    this.setOptionsShow = function(option, item) {
            ko.applyBindingsToNode(option, {visible: item.show}, item);
        }

}

$(document).ready(function() {

    var vm = new viewModel();
    ko.applyBindings(vm);

})​

​
函数viewModel(){
var self=这个;
this.countryArray=ko.observeArray([{“name”:“France”,“show”:true},
{“名称”:“意大利”,“表演”:真实},
{“名称”:“德国”,“显示”:假});
this.selectedCountry=ko.可观察(“”);
}
$(文档).ready(函数(){
var vm=new viewModel();
ko.应用绑定(vm);
})​
签出此

您可以这样做:

 <select  data-bind="value: selectedCountry , foreach : countryArray" >
       <!-- ko if: show -->        
            <option data-bind="value : name, text : name "></option>
       <!-- /ko -->
 </select> ​

function viewModel() {

    var self = this;
    this.countryArray = ko.observableArray([{"name" : "France" ,"show" : true},
                                            {"name" : "Italy" , "show" : true},
                                            {"name":"Germany" , "show" : false}]);

    this.selectedCountry = ko.observable("");

}

$(document).ready(function() {

    var vm = new viewModel();
    ko.applyBindings(vm);

})​
 <select  data-bind="value: selectedCountry , options : countryArray, optionsText: 'name', optionsAfterRender: setOptionsShow" ></select>  ​

function viewModel() {

    var self = this;
    this.countryArray = ko.observableArray([{"name" : "France" ,"show" : true},
                                            {"name" : "Italy" , "show" : true},
                                            {"name":"Germany" , "show" : false}]);

    this.selectedCountry = ko.observable("");
    this.setOptionsShow = function(option, item) {
            ko.applyBindingsToNode(option, {visible: item.show}, item);
        }

}

$(document).ready(function() {

    var vm = new viewModel();
    ko.applyBindings(vm);

})​

​
函数viewModel(){
var self=这个;
this.countryArray=ko.observeArray([{“name”:“France”,“show”:true},
{“名称”:“意大利”,“表演”:真实},
{“名称”:“德国”,“显示”:假});
this.selectedCountry=ko.可观察(“”);
}
$(文档).ready(函数(){
var vm=new viewModel();
ko.应用绑定(vm);
})​
试试这个

这里有一个2017年的更新:实现这一点的最佳方法(特别是在没有无容器条件绑定的情况下)来自选项绑定的淘汰文档,使用optionAfterRender绑定。在版本2.3中添加了选项AfterRender

您的代码如下所示:

 <select  data-bind="value: selectedCountry , foreach : countryArray" >
       <!-- ko if: show -->        
            <option data-bind="value : name, text : name "></option>
       <!-- /ko -->
 </select> ​

function viewModel() {

    var self = this;
    this.countryArray = ko.observableArray([{"name" : "France" ,"show" : true},
                                            {"name" : "Italy" , "show" : true},
                                            {"name":"Germany" , "show" : false}]);

    this.selectedCountry = ko.observable("");

}

$(document).ready(function() {

    var vm = new viewModel();
    ko.applyBindings(vm);

})​
 <select  data-bind="value: selectedCountry , options : countryArray, optionsText: 'name', optionsAfterRender: setOptionsShow" ></select>  ​

function viewModel() {

    var self = this;
    this.countryArray = ko.observableArray([{"name" : "France" ,"show" : true},
                                            {"name" : "Italy" , "show" : true},
                                            {"name":"Germany" , "show" : false}]);

    this.selectedCountry = ko.observable("");
    this.setOptionsShow = function(option, item) {
            ko.applyBindingsToNode(option, {visible: item.show}, item);
        }

}

$(document).ready(function() {

    var vm = new viewModel();
    ko.applyBindings(vm);

})​
​
函数viewModel(){
var self=这个;
this.countryArray=ko.observeArray([{“name”:“France”,“show”:true},
{“名称”:“意大利”,“表演”:真实},
{“名称”:“德国”,“显示”:假});
this.selectedCountry=ko.可观察(“”);
this.setoptionshow=函数(选项,项){
applybindingstoode(选项,{visible:item.show},item);
}
}
$(文档).ready(函数(){
var vm=new viewModel();
ko.应用绑定(vm);
})​
试试这个

这里有一个2017年的更新:实现这一点的最佳方法(特别是在没有无容器条件绑定的情况下)来自选项绑定的淘汰文档,使用optionAfterRender绑定。在版本2.3中添加了选项AfterRender

您的代码如下所示:

 <select  data-bind="value: selectedCountry , foreach : countryArray" >
       <!-- ko if: show -->        
            <option data-bind="value : name, text : name "></option>
       <!-- /ko -->
 </select> ​

function viewModel() {

    var self = this;
    this.countryArray = ko.observableArray([{"name" : "France" ,"show" : true},
                                            {"name" : "Italy" , "show" : true},
                                            {"name":"Germany" , "show" : false}]);

    this.selectedCountry = ko.observable("");

}

$(document).ready(function() {

    var vm = new viewModel();
    ko.applyBindings(vm);

})​
 <select  data-bind="value: selectedCountry , options : countryArray, optionsText: 'name', optionsAfterRender: setOptionsShow" ></select>  ​

function viewModel() {

    var self = this;
    this.countryArray = ko.observableArray([{"name" : "France" ,"show" : true},
                                            {"name" : "Italy" , "show" : true},
                                            {"name":"Germany" , "show" : false}]);

    this.selectedCountry = ko.observable("");
    this.setOptionsShow = function(option, item) {
            ko.applyBindingsToNode(option, {visible: item.show}, item);
        }

}

$(document).ready(function() {

    var vm = new viewModel();
    ko.applyBindings(vm);

})​
​
函数viewModel(){
var self=这个;
this.countryArray=ko.observeArray([{“name”:“France”,“show”:true},
{“名称”:“意大利”,“表演”:真实},
{“名称”:“德国”,“显示”:假});
this.selectedCountry=ko.可观察(“”);
this.setoptionshow=函数(选项,项){
applybindingstoode(选项,{visible:item.show},item);
}
}
$(文档).ready(函数(){
var vm=new viewModel();
ko.应用绑定(vm);
})​

Sry,我正在使用linux无法测试IE。您是否收到一些错误?无容器条件绑定被忽略。。。似乎其他人都有这个问题:Sry,我正在使用linux无法测试IE。您是否收到一些错误?无容器条件绑定被忽略。。。似乎其他人也有这个问题: