Javascript从多个选择框中获取值

Sor*_*ren 21 javascript multiple-select drop-down-menu

这个让我疯了.它是一个简单而愚蠢的东西,我在俯视.我在表单中有一个多选框.我只是想获取所选的值.在我的循环中,如果我使用警报,那么我没有问题.一旦尝试连接值我得到错误'SelBranch [...] .selected'是null或不是对象

      <form name="InventoryList" method="post" action="InventoryList.asp">
          <select name="SelBranch" class="bnotes" size="5" multiple="multiple">
          <option value="All">All</option>
          <option value="001 Renton">001 Renton</option>
          <option value="002 Spokane">002 Spokane</option>
          <option value="003 Missoula">003 Missoula</option>
          <option value="004 Chehalis">004 Chehalis</option>
          <option value="005 Portland">005 Portland</option>
          <option value="006 Anchorage">006 Anchorage</option>
          <option value="018 PDC">018 PDC</option>
          </select>

         <input type="button" name="ViewReport" value="View" class="bnotes" onclick="GetInventory();">

   </form>


   <script language="JavaScript">
       function GetInventory()
       {
         var InvForm = document.forms.InventoryList;
         var SelBranchVal = "";
         var x = 0;

         for (x=0;x<=InvForm.SelBranch.length;x++)
         {
            if (InvForm.SelBranch[x].selected)
            {
             //alert(InvForm.SelBranch[x].value);
             SelBranchVal = SelBranchVal + "," + InvForm.SelBranch[x].value;
            }
         }
         alert(SelBranchVal);
       }


  </script>
Run Code Online (Sandbox Code Playgroud)

ami*_*t_g 17

for循环正在进行一次额外的运行.更改

for (x=0;x<=InvForm.SelBranch.length;x++)
Run Code Online (Sandbox Code Playgroud)

for (x=0; x < InvForm.SelBranch.length; x++)
Run Code Online (Sandbox Code Playgroud)