element in form is empty in IE
The following code is working in FF but not in IE. There is no value of document.myform["parameters"].value
in IE.
The code:
function form_elements()
{
if(document.myform.elements["parameters"].value == '')
{
myform_values = ......
}
}
<form method="post" name="myform">
<div id="parameters_info">
<select id="parameters" name="parameters" onfocus="e =form_elements();" >
<?php
foreach($params_name as $name)
{
if($name == $param_posted)
开发者_如何学编程 {
echo "<option selected>$name</option>";
}
else
{
echo "<option>$name</option>";
}
}
?>
</select>
</div>
</form>
I tried also document.myform["parameters"].value
but the value is empty.
My options are like:
<option>1234</option>
<option>234a</option>
And i want that the value of the option will be in the function.
10x, Ronny
You need to give a value to each option. Also, you cannot get the selected option using the value of the select tag. You need to use:
var selectedOption = document.myform.parameters.options[document.myform.parameters.options.selectedIndex].value;
<option>1234</option>
1234
- is a text
of an option.
<option value="1234">1234</option>
Now 1234
- is a text and value.
That should be:
document.forms["myform"].elements["parameters"].options[document.forms["myform"].elements["parameters"].selectedIndex].text
...or you can use a function like this, which returns an array of values from any field type:
function getFieldValueArray(fld, frm) {
// fld may be a field object or a field name
// frm may be an index or a name, defaults to document.forms[0] when needed and not supplied
//function returns an array of values
var form = frm ? document.forms[frm] : document.forms[0];
var field = (typeof(fld) == "object") ? fld : form.elements[fld];
var valueArray = new Array();
var multinode = (field.length>1) ? true : false;
var type = field.type;
if (!type) {
type = field[0].type;
}
switch(type) {
case "radio": //fall-through intentional
case "checkbox":
if (multinode) {
for (i=0; i<field.length; i++) {
if (field[i].checked) {
valueArray.push(field[i].value);
}
}
}
else {
if (field.checked) {
valueArray.push(field.value);
}
}
break;
case "select-one": //fall-through intentional
case "select-multiple":
var options = field.options;
for (i=0; i<options.length; i++) {
if (options[i].selected) {
valueArray.push(options[i].value ? options[i].value : options[i].text);
}
}
break;
case "text": //fall-through intentional
case "file":
valueArray.push(field.value);
break;
case "hidden":
valueArray = field.value.split(";");
break;
case "textarea":
valueArray = field.value.split("\n");
default:
alert("Field not found -- atGetField(" + fld.toString() + ")");
}
return valueArray;
}
The function comes in rather handy when you can't be certain what type the field may be (for instance, if the field is an editable type under some circumstances and hidden under others), but once written it's an easy fallback, particularly for selects, radios and checkboxes.
精彩评论