- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有使用 knockout 的 View 和绑定(bind),我试图用我的可观察数组中的值填充我的下拉菜单,但我的 html 以某种方式覆盖它并显示默认字符串。
我的 HTML
<table id="tblBrands">
<tr data-bind="foreach: agency">
<td>
<table>
<tr>
<td>Advertiser Name
</td>
<td>Account Name
</td>
<td>Responsibility
</td>
<td>Full Name
</td>
</tr>
<tbody data-bind="foreach: brands">
<tr>
<td>
<input data-bind="value: advertiserName" /></td>
<td>
<input data-bind="value: brandName" /></td>
<td>
<!-- <select data-bind="value: responsibility" />-->
<select data-bind="value: responsibility">
<option value="">Select...</option>
<option value="">Media Manager</option>
<option value="">Strategic Responsibility</option>
<option value="">Planning/Buying</option>
</select>
</td>
<td>
<input data-bind="value: fullName" /></td>
<td><a href='#' data-bind='click: $root.removeBrand' style="color: blue">Remove</a></td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
我正在尝试将其绑定(bind)到下拉菜单:
<select data-bind="value: responsibility">
代理JS
我的机构 View 模型很大,但它是这样工作的:
define(['services/datacontext'], function (dataContext) {
var initialized = false;
var agency;
agency = ko.observableArray([]);
var save = function () {
// Clear Cache because user submitted the form. We don't have to hold onto data anymore.
localStorage.setItem('Agency', null);
localStorage.setItem('Offices', null);
localStorage.setItem('Brands', null);
var agency = ko.toJS(vm.agency._latestValue[0]) //Drill down to _latestValue and convert this to standard JS object
var s = YUI().use("json-stringify", function (Y) {
var jsonAgency = Y.JSON.stringify(agency, [
"activities",
"agencyName",
"agencyID",
"category",
"declaredBillings",
"immediateParent",
"numberOfEmployees",
"ultimateParent",
"uRL",
"offices",
"address1",
"address2",
"address3",
"address4",
"address5",
"agencyID",
"faxNumber",
"officeID",
"postCode",
"telephoneNumber",
"contacts",
"emailAddress",
"firstName",
"jobName",
"personID",
"surName",
"title",
"brands",
"agencyId",
"brandId",
"brandName",
"categoryDescription",
"dateAmended",
"activities",
"additionalInfo",
"advertiserId",
"advertiserName",
"alfRank",
"cinemaRank",
"directMailRank",
"internetRank",
"nielsenId",
"notes",
"numberOfEmployees",
"outdoorRank",
"pressRank",
"radioRank",
"tVRank"
]);
dataContext.saveChanges(jsonAgency, localStorage.setItem('AgencyCurrent', jsonAgency)); // Pass current data from cache and new data from form
})
}
var vm = { // This is my view model, my functions are bound to it.
//These are wired up to my agency view
activate: activate,
agency: agency,
title: 'agency',
refresh: refresh, // call refresh function which calls get Agencies
save: save,
cacheForm: cacheForm,
addOffice: addOffice,
removeOffice: removeOffice,
addBrand: addBrand,
removeBrand: removeBrand,
addContact: addContact,
removeContact: removeContact
};
return vm;
function activate() {
vm.agency;
if (initialized) {
return;
}
initialized = false;
refresh();
}
function refresh() {
dataContext.getAgency(agency);
}
function cacheForm(agency) {
var agency = ko.toJS(vm.agency._latestValue[0]) //Drill down to _latestValue and convert this to standard JS object
//Check if the current data has been cached before caching the new data, otherwise just cache the new data.
if (!localStorage.AgencyCurrent) {
var s = YUI().use("json-stringify", function (Y) {
var jsonAgency = Y.JSON.stringify(agency, [
"activities",
"agencyName",
"agencyID",
"campaignBillings",
"category",
"declaredBillings",
"immediateParent",
"numberOfEmployees",
"ultimateParent",
"uRL",
"offices",
"address1",
"address2",
"address3",
"address4",
"address5",
"agencyID",
"faxNumber",
"officeID",
"postCode",
"telephoneNumber",
"contacts",
"emailAddress",
"firstName",
"jobName",
"personID",
"surName",
"title",
"brands",
"agencyId",
"brandId",
"brandName",
"categoryDescription",
"dateAmended",
"activities",
"additionalInfo",
"advertiserId",
"advertiserName",
"alfRank",
"cinemaRank",
"directMailRank",
"internetRank",
"nielsenId",
"notes",
"numberOfEmployees",
"outdoorRank",
"pressRank",
"radioRank",
"tVRank"
]);
localStorage.setItem('AgencyCurrent', null);
localStorage.setItem('AgencyCurrent', jsonAgency);
})
}
var brands = ko.toJS(vm.brands);
var s = YUI().use("json-stringify", function (Y) {
var jsonStrAgency = Y.JSON.stringify(agency, [
"activities",
"agencyName",
"agencyID",
"campaignBillings",
"category",
"declaredBillings",
"immediateParent",
"numberOfEmployees",
"ultimateParent",
"uRL"
]); // Use an array of acceptable object key names as a whitelist.
var jsonStrOfficesContacts, jsonStrBrandsAdvertisers;
for (i in agency.offices) { // Outer loop for each office
jsonStrOfficesContacts = Y.JSON.stringify(agency.offices, [
"address1",
"address2",
"address3",
"address4",
"address5",
"agencyID",
"faxNumber",
"officeID",
"postCode",
"telephoneNumber",
"contacts", // From this point on I am drilling into office[n].contacts data
"emailAddress",
"firstName",
"jobName",
"personID",
"surName",
"title"
]);
}
jsonStrBrandsAdvertisers = Y.JSON.stringify(brands, [
"agencyId",
"brandId",
"brandName",
"categoryDescription",
"dateAmended",
"defaultSearchName1",
"defaultSearchName2",
"advertiser", //brands.advertser
"activities",
"additionalInfo",
"advertiserId",
"advertiserName",
"alfRank",
"cinemaRank",
"directMailRank",
"internetRank",
"nielsenId",
"notes",
"numberOfEmployees",
"outdoorRank",
"pressRank",
"radioRank",
"tVRank"
]);
localStorage.setItem('Agency', null);
localStorage.setItem('Offices', null);
localStorage.setItem('Brands', null);
localStorage.setItem('Agency', jsonStrAgency);
localStorage.setItem('Offices', jsonStrOfficesContacts);
localStorage.setItem('Brands', jsonStrBrandsAdvertisers);
});
//var objBrands = new Object(ko.mapping.fromJSON(localStorage.getItem('Brands')));
// for (i in objBrands._latestValue) {
// for (a in objBrands._latestValue[i]) {
// if (a == "advertiser") {
// objAdvertiser = objBrands._latestValue[i].advertiser; // get advertiser data
// objBrands.unshift(objBrands._latestValue[i].advertiser) // unshift it from root
// // create temp objects
// var tempBrands = ko.toJS(objBrands);
// var tempAdvertiser = ko.toJS(objAdvertiser);
// brands[i].advertiser = tempAdvertiser;
// }
// }
// }
}
function addOffice() {
//Convert agency observable to object
var objAgency = ko.toJS(vm.agency);
var objOffice = {
address1: " ",
address2: " ",
address3: " ",
address4: " ",
address5: " ",
agencyID: " ",
contacts: [],
faxNumber: " ",
officeID: " ",
postCode: " ",
telephoneNumber: " ",
}
objAgency[0].offices.unshift(objOffice); // Add new object to agency
vm.agency([ko.mapping.fromJS(objAgency[0])]); //Bind back to KO
}
function removeOffice(office) {
var ID = office.officeID._latestValue;
agency._latestValue[0].offices.remove(office);
}
function addContact(office) { // Passing in object array of agency. We no it contains correct office and agency ID
// Assign observable data to new variable then remove old
// variable from mapping
var objAgency = ko.toJS(agency);
var officeTableNum;
//vm.agency.removeAll();
var agencyID = office.agencyID._latestValue;
var officeID = office.officeID._latestValue
// Fill new object with empty strings and related data
var objContact = {
agencyID: agencyID,
emailAddress: "",
firstName: "",
jobName: "",
office: "",
OfficeID: officeID,
personID: "",
surName: "",
title: "",
responsibilities: "",
notes: ""
}
// Splice where office ID match
for (i in objAgency[0].offices) {
if (!isNaN(i)) {
if (objAgency[0].offices[i].officeID === officeID) {
// set var for tracking office i.e. office 1, 2, 3
officeTableNum = i + 1; // Because of 0 index, add 1
objAgency[0].offices[i].contacts.unshift(objContact); // At i remove one object
}
else {
}
}
}
vm.agency([ko.mapping.fromJS(objAgency[0])]);
}
function removeContact(contact) {
for (i in agency._latestValue[0].offices._latestValue) {
if (isNaN(i)) { // Escape if NaN, otherwise use index valI ha
}
else {
for (ii in agency._latestValue[0].offices._latestValue[i].contacts._latestValue) {
agency._latestValue[0].offices._latestValue[i].contacts.remove(contact);
}
}
}
}
function addBrand() {
//Convert agency observable to object
var objAgency = ko.toJS(vm.agency);
var objBrand = {
brandName: " ",
advertiserName: " ",
aLFRank: " ",
totalRank: " ",
cinemaRank: " ",
directMailRank: " ",
internetRank: " ",
outdoorRank: " ",
pressRank: " ",
radioRank: " ",
tVRank: " ",
}
objAgency[0].brands.unshift(objBrand); // Add new object to agency
vm.agency([ko.mapping.fromJS(objAgency[0])]); //Bind back to KO
}
function removeBrand(brand) {
var ID = brand.brandID._latestValue;
agency._latestValue[0].brands.remove(brand);
}
});
总结
想要将我的可观察数组数据绑定(bind)到下拉菜单,但也有可用的下拉项。
编辑
新虚拟机
var vm = { // This is my view model, my functions are bound to it.
//These are wired up to my agency view
activate: activate,
agency: agency,
title: 'agency',
refresh: refresh, // call refresh function which calls get Agencies
save: save,
cacheForm: cacheForm,
addOffice: addOffice,
removeOffice: removeOffice,
addBrand: addBrand,
removeBrand: removeBrand,
addContact: addContact,
removeContact: removeContact,
responsibilityOptions: ['France', 'Germany', 'Spain']
};
return vm;
新绑定(bind)
<select data-bind="options: $root.responsibilityOptions, value: responsibility"></select>
最佳答案
您应该为选择使用选项绑定(bind)。
文档位于:http://knockoutjs.com/documentation/options-binding.html
基本上你这样做:
<select data-bind="options: yourArray, value: responsibility"></select>
只需将 yourArray
替换为您想要填充下拉列表的内容即可。
因此与其将选项放入 HTML 中,不如将它们放入一个 observableArray 中并使用它代替 yourArray
看看这个乱七八糟的 fiddle ,看看它是否能帮助你:http://jsfiddle.net/r3AA9/
关于javascript - KnockOut.JS 使用 observable 的值填充下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19120123/
我有一个像这样的数组 var resultsArray = [ { name: "BMW", value: "BMW", text: "BMW" }, { name: "Mercedes-Benz",
我正在尝试实现发现的下拉检查列表 here在 ASP.NET ListBox 控件上。它将控件呈现为下拉列表,并应用所有 css。但是,这些选项不是预期的复选框,而是单选按钮。有谁知道为什么当我让它与
如何使用 Javascript 在下拉列表的更改事件中获取先前选择的索引。 最佳答案 不,这是不可能的,但您可以在 onchange 事件中使用一个变量来跟踪之前的选择 示例: var previou
我想使用一个名为 dropdown-check-list 的插件: http://code.google.com/p/dropdown-check-list/ 但是,它的某些功能似乎与谷歌浏览器不兼容
我正在尝试找出如何制作类似于苹果商店的过滤选项的过滤选项。我首先想到的是类似于网站的下拉列表。但xcode中的对象选项上似乎没有它。想知道我应该从哪里开始才能实现这种功能。 和这个类似 http://
我正在尝试为类别创建一个下拉列表。如果这检查没问题,那么它必须是数据库。 型号: 分类 var $hasMany = 'Product'; 产品 var $belongsTo = 'Category'
有六个问题要问用户。如果用户回答了这些问题,我正在尝试制作一个应用程序,该应用程序将确定在右侧使用哪种研究设计的结果。我正在用 python dash 做这个应用程序。我的 Python 代码如下。如
我的问题是我所问问题的延续,请参阅链接。 Load Country/State/City 我已经展开以从数据库加载我的下拉列表,我只需要一种方法在我的第一个下拉列表和第二个下拉列表中连接 onchan
我正在尝试为一家餐厅创建一个内部成本核算电子表格。我重新熟悉了如何创建下拉列表(在本例中用于选择成分)。 当有人选择例如从下拉列表中选择“胡萝卜”,我希望其他字段能够使用另一个电子表格中的成本数据自动
JavaScript/jQuery 新手。我在有序列表中显示了一些数据,如下所示 Data 0 Data 1. Da
我在其中一个主题上发现了这一点: http://jsfiddle.net/GHzfD/357/我想问一下从下拉列表中选择图像后如何提醒(路径)。 $("body select").msDropDow
我使用 JAVA Swing 创建了一个下拉列表。当我选择“跟踪 RCM 的状态:”时,我想在所选选项旁边创建另一个下拉列表。我应该使用 mouseactionlistener 代替吗?我试图完成类似
在 Symfony2 网站中,我尝试制作一个包含 2 个(或 3 个)下拉列表的表单,其依赖关系为国家 > 地区 > 城市。该城市是我正在使用表单编辑的元素的字段。这个想法是根据选择来填充列表。 我已
我正在尝试创建一个菜单来计算 的数量如果列表中的数量超过 5 个,请将其余的移动到下拉列表中。 基本上代码如下所示: Item 1 Item 2 Item 3 Item 4 I
当我点击要安装主题的部门时,当我点击主题时要安装的服务。但当我点击服务时却没有看到问题。我认为对json的描述不准确。你能帮我解决这个问题吗?谢谢。我的 Jquery 代码; /* Select';
我有一个包含两个值的下拉列表:Sponsor 和 Social_Worker。我想要做的是,当选择其中一个时,它会显示一个 div 并隐藏另一个 div,如果选择另一个则反之亦然。我设法使用按钮执行此
我正在创建 2 个下拉列表,第二个下拉列表基于对第一个下拉列表的选择。从mysql数据库中获取数据 索引.php P
我正在尝试使用 JS 创建互斥的下拉菜单。 只能从这 4 个操作系统中选择一个:image 当一个被选中时,其他的应该被禁用。 HTML 部分: Re
首先,我是 java 脚本的新手。我正在开发我的 Web 应用程序,我有一个下拉菜单,其中包含人员列表。使用它我知道如何传递一个人的选定值。但是我如何选择多个值(人名)并将该数据发送到后端实现。是否可
我正在使用 Laravel 框架,并且有两个下拉列表,它们都从数据库表中读取数据, 第一个它从表中读取所有记录并将其填充到选择列表中 这是我的代码: {{Form::select
我是一名优秀的程序员,十分优秀!