Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript从列表中获取项的值_Javascript - Fatal编程技术网

使用javascript从列表中获取项的值

使用javascript从列表中获取项的值,javascript,Javascript,我有以下代码: <script> function getgroup(){ var i=0; var total=document.getElementById("selectedOptions").length; while (i<total) { var group=document.getElementById("selectedOptions").value; var group2=group.subs

我有以下代码:

<script>
  function getgroup(){

        var i=0;
        var total=document.getElementById("selectedOptions").length;

    while (i<total)
  {
    var group=document.getElementById("selectedOptions").value;
    var group2=group.substring(2);

    alert(group2);
     i++;
}
}
</Script>

函数getgroup(){
var i=0;
var total=document.getElementById(“selectedOptions”).length;

(i我认为您使用的是
getElementById()
方法,根据规范,元素的ID应该是唯一的,因此长度始终为一。尝试添加一个类,并使用
getElementsByClass()

将select元素的值指定给组变量。您需要循环使用以下选项:

group = document.getElementById("selectedOptions").options[i].value;

应该只存在一个具有唯一id的对象。很明显,只有一个项。鉴于id为“selectedOptions”的元素是select,
length
将包含该select中的选项数。但这里没有提到它。我知道这一点。无论如何,感谢X只是一件小事,我想求和()组值,我做了
var-group3=group3+group2;
,但它没有计算您得到的结果是什么?您可能必须对变量使用
parseInt
,才能将它们相加。
var-group2=parseInt(group.substring(2))
我得到了NaN作为结果你的选项有什么值?如果你想把它们加在一起,它们必须是某种数字,如果你得到了一个
NaN
结果,它们就不是了。示例1:G.132,我得到了数字(132)-示例2:G.125等等