2013-04-10 130 views
4

我在我的網站中有一個頁面,其中包含一個iframe,iframe內有多個輸入框。我想要做的是,當有人向iframe輸入框中輸入一個值時,該值將被寫入父頁面的輸入框中。更改iframe中的輸入值,父級輸入更改

我發現放在這裏其他一些答案,但我不能設法讓他們以適合我的需要,所以到目前爲止,我有類似

HTML - 父

<form method="post" id="presentation-form" action=」action.php」> 
    <input type="text" id="edit-title" name="title"> 
    <iframe id="upload_frame" src="upload.html" ></iframe> 
    <input type="submit" id="edit-submit" name="op" value="Save"> 
</form> 

HTML - 爲孩子

<form name="formUpload" id="formUpload" etc..> 
    <input id=」mytitle」 type="text" size="50" name="title" value=""> 
    <input type="submit" name="submit" value="Upload File"> 
</form> 

我要的是,當我更改子窗體「mytitle」的值,它chanegs在父窗體的「編輯標題」

我的Jquery到目前爲止是

$('#upload_frame').contents().find("#formUpload").delegate('input', 'change', function(e){ 
    //code goes here 
}); 

我得到的問題是,即使當我試圖從腳本內發出警報,或登錄東西,什麼也沒有發生,我沒有得到任何JavaScript錯誤,但當我做了什麼

(有人問之前,這個孩子頁面是我同一個域內的頁面)

任何幫助就沒有得到警告不勝感激

回答

8

對於浮圖回覆人,這是我最終使用的解決方案,它似乎工作正常

$('iframe').load(function(){ 
    $('iframe').contents().find('input#mytitle').bind('change',function(e) { 
     title_name = $(this).val(); 
     $('input#edit-title').val(title_name); 
    }); 
}); 
+0

嘿@Andrew有什麼好運呢? http://stackoverflow.com/questions/40985005/how-to-run-js-code-after-form-iframe-loaded-and-place-custom-value-in-input – 2016-12-06 03:11:04