I am trying to set the val开发者_JS百科ues for a popup menu in Dashcode programmatically. I can change the text/value of some statically defined default ones (from the inspector), but not add or remove them. When the view is initialised it must take a variable number of options.
var popup = document.getElementById('popup');
//popup.options = []; /* Doesn't clear the list */
//popup.options.length = 0; /* Doesn't clear the list */
popup.options[0].text = "Option 1";
popup.options[0].value = "123";
How can I modify the list? (LMGTFY answers not required :)
I solved it like this in the end:
//remove all
if (popup.hasChildNodes()) {
while (popup.childNodes.length >= 1) {
popup.removeChild(popup.firstChild);
}
}
//add new
$.each(items, function(i, item) {
var option = document.createElement("option");
option.text = item.name;
option.value = item.id;
popup.appendChild(option);
});
Just a stab but couldn't you build the whole popup in JavaScript and set the number of items in the drop down / popup by passing parameters. Then when you want to change any of the items you can call the JavaScript with new parameters?
Or did i misunderstand the question.
when i try to bind the pop-up menu items dynamically similar as you do, i see the values i assign to pop-up menu items are correct, but non of the items are displayed in the pop-up.
Is there something i'm missing ? Here is my code:
var ddlCurrencyCode = document.getElementById('ddlCurrencyCode');
if (ddlCurrencyCode.hasChildNodes()) {
while (ddlCurrencyCode.childNodes.length >= 1) {
ddlCurrencyCode.removeChild(ddlCurrencyCode.firstChild);
}
}
for (var i = 0; i < result.rows.length; ++i) {
var row = result.rows.item(i);
var ddlItem = document.createElement("ddlItem");
ddlItem.text = row['key'];
ddlItem.value = row['value'];
ddlCurrencyCode.appendChild(ddlItem);
}
精彩评论