Javascript not acquiring dropdown value
I have a list of 2 values in dropdown list as...
<select id="prop">
<option value="Caa:123">开发者_如何学CCa</option>
<option value="Cb:745">Cb</option>
</select>
...and in javascript i used...
var p = document.getElementById('prop');
var q = p.options[p.selectedIndex].value;
alert(q);
...but I am not getting no alert and an error "Index or size is negative or greater than the allowed amount" code: "1 " Kindly help I trapped in this problem
Try like this:
var p = document.getElementById('prop');
var q = p.value;
alert(q);
This wasn't in your questions specifications, but I would use jQuery. It definitely is much more easier to handle and looks neater:
<select id="prop">
<option value="Caa:123">Ca</option>
<option value="Cb:745">Cb</option>
</select>
<script>
alert($('#prop').val());
$('#prop').change(function () {
alert($('#prop').val());
});
</script>
[ View output ]
精彩评论