Javascript Detect selected items
I have the following code. I need to know the selected fields. [] is needed for PHP. Without [] it works but in the following codes an error occurs.
<form name="tform">
<select multiple name="test[]">
<option selected>1</option><option>2</option></select></form>
<script type=text/javascript>
var x=document.tform.test;
for(v开发者_开发技巧ar i=0;i<x.length;i++) {
if (x.options[i].selected) {
xselected++;
}
}
alert(xselected);
</script>
Replace var x=document.tform.test;
with the following:
var x = document.tform.getElementsByName("test[]")[0];
This way you should be able to select the <select>
-element.
精彩评论