简体   繁体   中英

referencing the drop-down box

I have this code to verify that.

<script type="text/javascript"> <!-- alert("Testing"); --> </script>

This code now makes a popup box on form page. All working as expected! Now the problem I'm having is referencing the drop-down box. I thought the following code should have done the trick but no.

var element; var i=EG;
element=document.getElementById("client_country"); element.selectedIndex = i;

But I cant seem to get it to work. I think its due to the use of frames, anyone able to point me in the direction of where I'm going wrong?

var i = 'EG'; // string inside quotes
var element = document.getElementById("client_country");
element.value = i; // set the selected option

// selectedIndex retrieves the value
var selected_value = element.options[element.selectedIndex].value;

This assumes you have HTML similar to...

<select id="client_country">
    <option value="AG">value a</option>
    <option value="CG">value c</option>
    <option value="EG">value e</option>
</select>

Working demo on JSFiddle .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM