How to set value for the form element using mootools
I have a form that have input element and the select element with multi selec开发者_高级运维t. so how can I set value for the input element and select element in mootools.
basically - element.get("value");
returns the value for all inputs, selects and textareas, .set("value", value);
to set them (except for multiples)
it's more complicated for select with multiple - the above returns the FIRST selected only, you need to do element.getSelected().get("value")
to recieve an array of values (.getSelected() on its own will return the pointers to the actual option
elements)
and finally, to set multiple selections, only way i can think of atm is to write your own element prototype that walks through child nodes with the values as specified and does .set("selected", "selected")
let me know if you need any help writing the latter
For dropdown:
$('idOfSelect').selectedIndex = x;
For text:
$('IdOfInput').value = 'Foo';
The answer is correct about multiples, and needing to use selectEl.getChildren('option').each(function(option)...
but even for single selects, in IE (I'm testing this in IE9) if there is no "value" property in each option then Mootools fails, i.e.
selectEl.set('value', value)
does not work. In FF it works and in IE it works if you set the "value" property of each element.
My solution is:
selectEl.getElements('option').each(function(option) {
option.selected = option.get('value')==value
});
Note that option.get('value') works even in IE, where there is no value
property, Mootools supplies the option's text
精彩评论