2016-06-07 20 views
-1

我很難弄清楚我錯了什麼。我在網上搜索了幾個小時,但沒有運氣。這是我的情況。我正在運行移動設備應用程序,我正在將表單(本地)加載到iframe中。然後iam(onload)嘗試將值插入到我的表單文本框中。這裏是代碼使用javascript將值輸入到html表格中

<iframe name="myframe"id="myframe"  
src="file:///C:/Users/denise/Desktop/updateform.html" 
width=480px height=400px frameborder=1></iframe> 

以及JavaScript函數

function imLoaded(){ 
    var ifrm = document.getElementById('myframe'); 
    var form = doc.getElementById('updateform'); 
    form.document.getElementById("updateform").user.value = 'hello'; 
} 
+0

您是否嘗試更新iframe中的值?我相信你需要使用'contentDocument'來訪問東西。例如:'document.getElementById('myframe')。contentDocument.body' – AtheistP3ace

+2

'doc'? 'form.document'? '.user.value'?此代碼看起來非常隨機... – David

+1

@INOH試試我的答案它爲你工作。 – Ansari

回答

1

試試這個

var iframe = document.getElementById('myframe'); 
var innerDoc = (iframe.contentDocument) ? iframe.contentDocument : iframe.contentWindow.document; 
alert(innerDoc.getElementById("updateform").innerHTML); 
alert(innerDoc.getElementById("updateform")["user"].value); 

可以使用innerDocgetElementById獲得ID的任何元素

希望這會幫助你。

+0

這裏是我的表單,加載到我的iframe代碼中'

名稱: Password:
' – INOH

+1

@INOH你創建了一個'imLoaded'函數,只是註釋這個函數中的所有行,並將它粘貼到你已經添加了這個iframe的頁面上('myframe'使用