在选择列表中使用javascript创建的属性 [英] Use created attribute with javascript in select list

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

问题描述

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

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

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

<input type="text" id="test" car="red" value="create Attribute test" size="40"/> 
<input type="button" value="submit" onclick="updateUrl();"> 

它一直给我未定义。如何从属性车中获取红色字符串?

it keep giving me undefined. how do i get the string red from attribute car?

编辑。我尝试使用选择列表,它现在提醒null

edit. i tried it with the select list it alerts null now

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


推荐答案

试试这个:

var newUrl = document.getElementById('test').getAttribute('car');

编辑

对于< select> ,您必须查看所选的< option> 元素,而不是< select> 本身:

For the <select>, you have to look into the selected <option> element, not the <select> itself:

var select = document.getElementById('test');
select.options[select.selectedIndex].getAttribute('car');

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

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