开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜