使用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等等