標籤:bind body query event ons this charset 資料 jquery
<!DOCTYPE html> <html><head> <meta charset="utf-8" /> <title>select</title> <script src="jquery-1.10.2.min.js"></script> <script type="text/javascript" src="knockout-3.2.0.js"></script> <script> function MyViewModel() { var self = this; //用self替代this self.peopleList = ko.observableArray([ { name: "張三", id: "1" }, { name: "李四", id: "2" }, { name: "王五", id: "3" }]); //監控數組self.selectedPeople=ko.observable(0); //記錄已選中的人員值self.peopleChange=function() //選擇某個選項後的事件。{alert(self.selectedPeople()); } } $(function () { ko.applyBindings(new MyViewModel()); }); </script> </head><body><div><select data-bind="options: peopleList, optionsText: ‘name‘, optionsValue:‘id‘,value: selectedPeople, optionsCaption: ‘選擇人員...‘,event: { change: peopleChange}"></select></div></body></html>
=============================================================
options: peopleList 指定綁定資料有哪個對象來。optionsText: ‘name‘ 綁定時顯示欄位為nameoptionsValue:‘id‘ 綁定時值欄位為idvalue: selectedPeople 值儲存在哪個對象中optionsCaption: ‘選擇人員...‘ 預設下拉框中的文字event: { change: peopleChange}" 指定onchange事件需要執行的方法
=============================================================
kojs select綁值(工作)