简体   繁体   English

从中提取值 <select>并将它们放入数组

[英]Extracting values from <select> and putting them into an array

Surely, this shouldn't be so hard? 当然,这应该不难吗?

I have a <select> , which has, of course, <options> . 我有一个<select> ,当然还有<options> These options are always in number format, because they are dynamically added to the list by the user. 这些options 始终为数字格式,因为它们是由用户动态添加到列表中的。

I then need to get all of these options from the list, put them an array and then perform logic on the array. 然后,我需要从列表中获取所有这些选项,将它们放入一个数组,然后在该数组上执行逻辑。 I've tried searching around, but everything relates to jquery or php - and I'm using plain old HTML and JavaScript. 我尝试过搜索,但是所有内容都与jquery或php有关-而且我使用的是普通的旧HTML和JavaScript。

The select is in a scrolling-box format: select采用滚动框格式:

<select id="selectBox" name="select" size="15" style="width:190px;">  
<!-- <options> are added via javascript -->     
</select>

Currently, I'm using this JavaScript to get the elements, but it's not working: 当前,我正在使用此JavaScript来获取元素,但是它不起作用:

//Calculate all numbers
        var x=[];
    function calculate()
    {
        for (var i = 0; i < 999; i++)
        {
            x[i]=selectbox.options[i].value;
            alert(x[i]);
        }

    }

Calculate() is called by a button. Calculate()由按钮调用。 Something is going terribly wrong, and I can't work it out. 发生了严重错误,我无法解决。 selectbox is previously defined as var selectbox = document.getElementById("selectBox"); selectbox先前定义为var selectbox = document.getElementById("selectBox"); and I know this works. 而且我知道这可行。

The alert is only being called so I can try to debug the thing... 警报只是被调用,所以我可以尝试调试它。

I'm using the figure of 999 because I can't work out how to get a number of how many elements are in the <select> (because it is in scrolling-box format). 我使用999的数字是因为我不知道如何在<select>获取多少个元素(因为它是滚动框格式)。 The solution must be javascript , and the listbox must be in that scrolling-box format. 解决方案必须javascript ,并且列表框必须采用该滚动框格式。

Thanks in advance for your help! 在此先感谢您的帮助!

Edit -- Okay, more coding to help this. 编辑 -好的,更多编码可以帮助您。

<form id="aggregateForm">
    <input id="inputNum" value="" type="text"><input id="addnum" value="Add" onclick="add();" type="button">
    <br>
    <select id="selectBox" name="select" size="15" style="width:190px;">  

    </select>
    <br>
    <input id="calculate" value="Calculate" onclick="calculate();" type="button"><input id="reset" value="Reset" onclick="reset();" type="button">
</form>

<script type="text/javascript">
    var selectbox = document.getElementById("selectBox");

    function add()
    {
    //Function to add a new number to the list of digits
    //Parses an integer to ensure everything works okay
    if(IsNumeric(document.getElementById("inputNum").value) == true)
    {
        selectbox.options[selectbox.options.length] = new Option(document.getElementById("inputNum").value, document.getElementById("inputNum").value);
        inputNum.focus();
    }
    else if(IsNumeric(document.getElementById("inputNum").value) == false)
    {
        alert("I'm sorry, but you have entered an invalid number. Please enter a number into the input box.");
        inputNum.focus();
    }
    }

    //Calculate all numbers
    var x=[];
    function calculate()
    {
    for (var i = 0; i <selectbox.options.length; i++)
        {
    x[i]=selectbox.options[i].value;
    alert(x[i]);
        }


    }

    //IsNumeric function coding taken from http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric, code by Joel Coehoorn
    function IsNumeric(input)
    {
        return (input - 0) == input && input.length > 0;
    }


</script>

check this jsfiddle 检查这个jsfiddle

var selectbox = document.getElementById("selectBox");

var x = [];

function calculate()
{
    for (var i = 0; i <selectbox.options.length; i++)
    {
        x[i]=selectbox.options[i].value;
        alert(x[i]);
    }

}
calculate();

This will alert EVERY option element in the select. 这将提醒选择中的每个选项元素。

The problem is that calculate is the ID of the element too. 问题在于,calculate也是元素的ID。 And oddly enough it believes that calculate is that DOM object not you function: proof . 奇怪的是,它认为计算是DOM对象而不是您起作用的: 证明 I changed the function name to calculates . 我将函数名称更改为calculates

I only found out last week that you can reference your elements with IDs with said IDs. 我上周才发现,您可以使用带有上述ID的ID来引用您的元素。

<div id="really">Yep for some reason</div>

... later in javascript ...稍后在javascript中

// no document.getElementById, just
// let me be very clear, THIS IS WRONG TO USE VERY BAD SO EVERYONE CAN KNOW NOT TO USE THIS
// but it does work this way so be aware
really.innerHTML = "I guess he was right.";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM