js获取Listbox选择的值

将Listbox的 SelectionMode="Multiple",设置为多选时,用JS获取多选的值。
<script type="text/javascript">
     function GetValue() {
         var strlist = document.getElementById("ListBox1");//获取Listbox
         var str= "";

         //遍历Listbox,取得选中项的值
         if (strlist.options.length > 0) {
             for (var i = 0; i < strlist.options.length; i++) {
                 if (strlist.options[i].selected == true) {
                     var j = strlist.options[i].value;
                     str+=j+","; //把Value值串起来

                 }
             }
            var strValue=str.replace(/,$/, ""); //去掉最后一个逗号

           alert(strValue);
                     }
         else {
             alert("No Item in Listbox");
         }
     }
 </script>
posted @ 2010-04-15 10:49  szny  阅读(590)  评论(0编辑  收藏  举报