Javascript 从多选选项框中获取值

发布于 2024-10-23 13:31:50 字数 1554 浏览 1 评论 0原文

这让我发疯。这一定是我所忽略的简单而愚蠢的事情。 我的表单中有一个多重选择框。我只是想获取所选的值。在我的循环中,如果我使用警报那么我就没有问题。一旦尝试连接这些值,我就会收到错误“SelBranch[...].selected”为空或不是对象

      <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>

This one is driving me nuts. It’s got to be something simple and stupid that I am overlooking.
I have a multiple select box in a form. I am just trying to get the values that are selected. In my loop, if I use alert then I have no problem. As soon as try to concatenate the values I get the error ‘SelBranch[...].selected' is null or not an object

      <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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝眼泪 2024-10-30 13:31:51

for 循环额外运行了一次。更改

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

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

The for loop is getting one extra run. Change

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

to

for (x=0; x < InvForm.SelBranch.length; x++)
悲歌长辞 2024-10-30 13:31:51

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

<!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>

Here i am posting the answer just for reference which may become useful.

<!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>
楠木可依 2024-10-30 13:31:51

查看 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>]

这也适用于非多个


警告:似乎支持此selectedOptions目前尚不清楚

Take a look at 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>]

This would also work on non-multiple <select> elements


Warning: Support for this selectedOptions seems pretty unknown at this point

面犯桃花 2024-10-30 13:31:51

另外,将其更改为:

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

原因

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

是第一次变量 SelBranchVal 将为空

Also, change this:

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

to

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

The reason is that for the first time the variable SelBranchVal will be empty

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文