2011-12-27 174 views
0

如果我爲IE運行相同的代碼,每件事情都很好我將獲取selectedId的值,其中firefox和chrome的值爲Undefine。瀏覽器獨立問題代碼適用於IE瀏覽器,但不適用於Firefox和Chrome

----------------- code ------------------------------- 

<html> 
<head> 
<script type="text/javascript"> 

function createSelected() 
{ 
var value; 
var theForm = document.hell; 

for (var i = 0; i < theForm.length; i++) 
{ 
var e = theForm.elements[i]; 
if ((e.type == "hidden") && (e.value == "false")) 
{ 
     console.log("the value of selected IDS="+e.selectedId); 
    if (e.selectedId!= undefined) 
    { 
     Value = ", "+e.selectedId; 

    } 
} 
} 
console.log(Value); 
} 


</script> 
</head> 
<body> 
<form name="hell"> 

<h1>This working only with IE not with FireFox and Crome </h1> 
<br/> 
<br/> 
<input type="hidden" selectedId="heyya1" name="item1" value="false">h1</input> 
<input type="hidden" selectedId="heyya2" name="item2" value="false">h2</input> 
<input type="hidden" selectedId="heyya3" name="item3" value="false">h3</input> 
<input type="hidden" selectedId="heyya4" name="item4" value="false">h4</input> 
<input type="hidden" selectedId="heyya5" name="item5" value="false">h5</input> 
<input type="hidden" selectedId="heyya6" name="item6" value="false">h6</input> 
<input type="button" onclick=createSelected() value="find the values"></input> 
</form> 


</body> 

---------------------------code end----------------------------------------- 

請幫助這個爲什麼不能,我們在Firefox使用其他參數(如Selectedid HTML標籤內),就像我們可以在IE做到這一點。

在此先感謝..

+0

是否真的有效?我想知道。 – 2011-12-27 05:40:25

+0

爲什麼不只是一個簡單的'id = heyya ...'而不是這個自定義屬性? – Andreas 2011-12-27 05:56:00

+0

它的工作IE瀏覽器,但不是爲了狐狸。 – ifti 2011-12-27 06:22:29

回答

1

在Chrome中(對不起,我正在使用沒有FF的計算機,而且我不打算只爲這個問題安裝它),如果您使用.getAttribute(),它將起作用。所以:

// replace 
e.selectedId; 
// with 
e.getAttribute("selectedId"); 

或者,在你的函數:

var id = e.getAttribute("selectedId"); 
console.log("the value of selected IDS="+id); 
if (id != null) 
{ 
    value += ", "+id; 
} 

另外,請注意JavaScript是大小寫敏感的,所以你的變量value是不一樣的Value,和你說Value =,你可能意思是value +=。你可能想要在元素屬性周圍使用引號:onclick="createSelected()"

+0

感謝它現在的工作。 – ifti 2011-12-27 06:21:05

0

嘗試讀取selectedId如下。使用getAttribute方法

alert(「所選IDS的值=」+ e.getAttribute('selectedId'));

相關問題