2012-04-29 15 views
1

即時通訊設法訪問我在選擇列表中創建的屬性。在選擇列表中使用javascript創建屬性

<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();"> 

它不斷給我定義。我如何從屬性車中得到字符串紅色?

編輯。我選擇列表中則會發出警報空現在

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

請不要發表兩次相同的問題。 – 2012-04-30 15:48:00

回答

8

試試這個試了一下:

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

編輯

對於<select>,你必須尋找到選定<option>元素,不<select>本身:

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

不,試試這個http://stackoverflow.com/a/10369746/908879 – ajax333221 2012-04-29 04:51:57

+0

@ ajax333221您鏈接的問題應該關閉。 – bfavaretto 2012-04-29 07:13:18

相關問題