Javascript从多个选择框中获取值

时间:2011-03-16 18:31:57

标签: javascript drop-down-menu multiple-select

这个让我疯了。它是一个简单而愚蠢的东西,我在俯视。 我在表单中有一个多选框。我只是想获取所选的值。在我的循环中,如果我使用警报,那么我没有问题。一旦尝试连接值我得到错误'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>

4 个答案:

答案 0 :(得分:17)

for循环正在进行一次额外的运行。变化

for (x=0;x<=InvForm.SelBranch.length;x++)

for (x=0; x < InvForm.SelBranch.length; x++)

答案 1 :(得分:3)

我在这里发布的答案仅供参考,可能会有用。

<!DOCTYPE html>
<html>
<head>
<script>
function show()
{
     var InvForm = document.forms.form;
     var SelBranchVal = "";
     var x = 0;
     for (x=0;x<InvForm.kb.length;x++)
         {
            if(InvForm.kb[x].selected)
            {
             //alert(InvForm.kb[x].value);
             SelBranchVal = InvForm.kb[x].value + "," + SelBranchVal ;
            }
         }
         alert(SelBranchVal);
}
</script>
</head>
<body>
<form name="form">
<select name="kb" id="kb" onclick="show();" multiple>
<option value="India">India</option>
<option selected="selected" value="US">US</option>
<option value="UK">UK</option>
<option value="Japan">Japan</option>
</select>
<!--input type="submit" name="cmdShow" value="Customize Fields"
 onclick="show();" id="cmdShow" /-->
</form>
</body>
</html>

答案 2 :(得分:3)

看看HTMLSelectElement.selectedOptions

HTML

<select name="north-america" multiple>
  <option valud="ca" selected>Canada</a>
  <option value="mx" selected>Mexico</a>
  <option value="us">USA</a>
</select>

的JavaScript

var elem = document.querySelector("select");

console.log(elem.selectedOptions);
//=> HTMLCollection [<option value="ca">Canada</option>, <option value="mx">Mexico</option>]

这也适用于非multiple <select>元素


警告:支持此selectedOptions此时似乎还不得而知

答案 3 :(得分:2)

另外,改变这个:

    SelBranchVal = SelBranchVal + "," + InvForm.SelBranch[x].value;

    SelBranchVal = SelBranchVal + InvForm.SelBranch[x].value+ "," ;

原因是变量SelBranchVal第一次为空