2011-08-05 77 views
0

我有一些問題將字符串變量傳遞給方法RequirementPopup。這個方法需要三個參數id,typeId和objType(例如一個對象的類型,例如它是「Item」)爲什麼我得到[對象HTMLImageElement]?

第一個方法其中是執行其他方法的按鈕,其中傳遞了三個變量。

function EditForm(count, id, typeId, nr, objType, name, lat, lon) { 
    /* Here is something else */ 
<p><input type="button" id="edit-requirement" value="Edit requirement" onclick="javascript:RequirementPopup('+id+','+typeId+','+objType+')"/></p> 

} 

其假設,以顯示我們的變量,是通過第二種方法:

function RequirementPopup(id, typeId, objType) { 
/* Here is something else */ 
       document.getElementById("id").value = id; 
       document.getElementById("tid").value = typeId; 
       document.getElementById("oType").value = objType; 


} 

在DIV ID和TID我得到正確的變量,但在oType我得到[對象HTMLImageElement],而不是「項目」?

PS當我EditForm方法警報顯示OBJTYPE比如它是正確的,但是當我將它傳遞給RequirementPopup方法我越來越像[對象HTMLImageElement]

誰能告訴我,爲什麼它是發生和如何解決這個問題?

回答

0

您不會在EditForm中生成的JavaScript表達式的id,typeId和objType值中放置任何引號。如果id和typeId碰巧是數字值,那麼它們按預期發佈,因爲這些數字在JavaScript表達式中被解釋,該表達式將RequirementPopup作爲數字進行調用。但是,如果objType是一個像「Item」這樣的名稱,那麼調用RequirementPopup的JavaScript表達式會將其解釋爲符號而不是字符串,並且傳遞給RequirementPopup的參數將是該符號所指定的任何參數。

試試這個:

function EditForm(count, id, typeId, nr, objType, name, lat, lon) { 
    /* ... */ 
    var inputHtml = 
     '<p><input type="button" id="edit-requirement" value="Edit requirement" onclick="' + 
     'javascript:RequirementPopup(' + id + ',' + typeId +',' + 
     "'" + objType + "'" + 
     ')"/></p>'; 
} 
相關問題