这几天没事学习了下knockout.js,感觉很强大。
Source code: View Province:
<select data-bind="options: arrayPro,
optionsText: 'name',
value: selectedPro,
event: { change: myChange },
optionsCaption: '---请选择---'"></select>
City:
<select data-bind="options: arrayBindCity,
optionsText: 'name',
optionsCaption: '---请选择---'"></select>
Source code: View model<script type="text/javascript">
var viewModel = {
arrayPro: ko.observableArray([
{ pid: 1, name: "湖北省" },
{ pid: 2, name: "湖南省" },
{ pid: 3, name: "广东省" }
]),
selectedPro: ko.observable(),
arrayAllCity: ko.observableArray([
{ cid: 1, name: "武汉市" }, { cid: 1, name: "宜昌市" }, { cid: 1, name: "荆州市" },
{ cid: 2, name: "长沙市" }, { cid: 2, name: "株洲市" }, { cid: 2, name: "岳阳市" },
{ cid: 3, name: "广州市" }, { cid: 3, name: "珠海市" }, { cid: 3, name: "韶关市" }
]),
arrayBindCity: ko.observableArray(),
myChange: function () {
this.arrayBindCity.removeAll();
for (var i = 0; i < this.arrayAllCity().length; i++) {
var item = this.arrayAllCity()[i];
if (this.selectedPro() && this.selectedPro().pid == item.cid) {
this.arrayBindCity.push(item);
}
}
}
}
ko.applyBindings(viewModel);
</script>
很简单的级联,数据也是写死的。不足之处请拍砖。javascripthtml
Source code: View Province:
<select data-bind="options: arrayPro,
optionsText: 'name',
value: selectedPro,
event: { change: myChange },
optionsCaption: '---请选择---'"></select>
City:
<select data-bind="options: arrayBindCity,
optionsText: 'name',
optionsCaption: '---请选择---'"></select>
Source code: View model<script type="text/javascript">
var viewModel = {
arrayPro: ko.observableArray([
{ pid: 1, name: "湖北省" },
{ pid: 2, name: "湖南省" },
{ pid: 3, name: "广东省" }
]),
selectedPro: ko.observable(),
arrayAllCity: ko.observableArray([
{ cid: 1, name: "武汉市" }, { cid: 1, name: "宜昌市" }, { cid: 1, name: "荆州市" },
{ cid: 2, name: "长沙市" }, { cid: 2, name: "株洲市" }, { cid: 2, name: "岳阳市" },
{ cid: 3, name: "广州市" }, { cid: 3, name: "珠海市" }, { cid: 3, name: "韶关市" }
]),
arrayBindCity: ko.observableArray(),
myChange: function () {
this.arrayBindCity.removeAll();
for (var i = 0; i < this.arrayAllCity().length; i++) {
var item = this.arrayAllCity()[i];
if (this.selectedPro() && this.selectedPro().pid == item.cid) {
this.arrayBindCity.push(item);
}
}
}
}
ko.applyBindings(viewModel);
</script>
很简单的级联,数据也是写死的。不足之处请拍砖。javascripthtml
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货