admin管理员组

文章数量:1426804

I have the following setup

"<form name=\"mylimit\" style=\"float:left\">
                <select name=\"limiter\" onChange=\"limit()\">
                <option selected=\"selected\">&nbsp;</option>"; ...

I wrote a js script to access the selected value of the selectField 'limiter' like so:

 var w = document.mylimit.limiter.selectedIndex;

var url_add = document.mylimit.limiter.options[w].value;

//On every browser i get the expected value except on Internet Explorer. think IExplorer dont like the bolded portion above. Any clue?

I have the following setup

"<form name=\"mylimit\" style=\"float:left\">
                <select name=\"limiter\" onChange=\"limit()\">
                <option selected=\"selected\">&nbsp;</option>"; ...

I wrote a js script to access the selected value of the selectField 'limiter' like so:

 var w = document.mylimit.limiter.selectedIndex;

var url_add = document.mylimit.limiter.options[w].value;

//On every browser i get the expected value except on Internet Explorer. think IExplorer dont like the bolded portion above. Any clue?

Share Improve this question edited Jan 31, 2010 at 15:11 John Conde 220k99 gold badges463 silver badges502 bronze badges asked Jan 31, 2010 at 10:25 AfameeAfamee 5,3309 gold badges41 silver badges44 bronze badges 1
  • Have you tried document.getElementById() instead? – Kris Walker Commented Jan 31, 2010 at 10:46
Add a ment  | 

3 Answers 3

Reset to default 4

IE is looking for the value attribute. It looks like other browsers are defaulting to the text displayed as the value if value="" is not found on the option tag. The following is what works on all major browsers.

<form name="mylimit" style="float:left">
    <select name="limiter" onChange="limit()">
        <option selected="selected" value='foo'>bar</option>
    </select>
</form>

<script>
    var index = document.mylimit.limiter.selectedIndex;
    var value = document.mylimit.limiter.options[index].value;
    alert(value); // Alerts 'foo'
</script>

This way you can have a seperate value and display for each option.

Did you try out one of these

document.mylimit.limiter.options[document.mylimit.limiter.selectedIndex].value

or

document.getElementById('limiter').options[document.getElementById('limiter').selectedIndex].value

This should help I think

document.getElementById("limiter").value works as well.

本文标签: htmlCan39t access the option value using javascript on Internet ExplorerStack Overflow