使用选择列表中的javascript获取属性值 [英] get attribute value with javascript in select list

查看:59
本文介绍了使用选择列表中的javascript获取属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
在选择列表中使用javascript创建的属性

我试图访问我在选择列表中创建的属性.

im trying to access an attribute that i created in a select list.

<script language="JavaScript">
function updateUrl()
    {
        var newUrl = document.getElementById('test').getAttribute('car');
        alert(newUrl);
    }
</script>

<select name= "test" id= "test" onChange= "updateUrl()">
     <option value="1" selected="selected" car="red">1</option> 
     <option value="2" car="blue" >2</option> 
     <option value="3" car="white" >3</option> 
     <option value="4" car="black" >4</option>    
</select>

它总是给我空值.我该如何从属性car中获取价值?

it keep giving me null. how do i get the value from attribute car?

推荐答案

如果要从所选选项的 car 属性中获取值,则:

If you meant get the value from the car attribute of the selected option:

//get the option's car attribute at selectedIndex
var car = document.getElementById('test').options[select.selectedIndex].car;

这篇关于使用选择列表中的javascript获取属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆