How can I Get input value with Prototype?
I have the following input without any form around it:
<input type="text" value="myValue" name="td_website static" class="td_inner_input">
How can I get the Input value with prototype? I tried 开发者_JAVA百科to use alert($('tb_website static').value);
, but it doesn't work.
alert($$('[name="td_website static"]')[0].value)
You need to use the $$
function which returns an array. There are a couple of ways to use an enumerable result.
If you know there will be only one matching element then use this:
$$('[name="tb_website static"]').first().value
If there are more than one input (which is valid HTML) then this gets an array of values:
$$('[name="tb_website static"]').map(Form.Element.getValue)
(By mapping through Form.Element.getValue
- aliased as $F
- it better handles browser differences and non-input
elements that don't store their value in a value
attribute)
I'm pretty sure that $('tb_website static')
will be looking for an element with that ID not that NAME.
Have another look at the PrototypeJS Documentation.
That because $()
function is an alias for getElementById()
. Which means you need to give and id in your input :)
<input type="text" value="myValue" id="td_website static" name="td_website static" class="td_inner_input">
精彩评论