开发者

Selecting a form which is in an iframe using jQuery

I have a form inside an iframe which is inside a jQuery UI dialog box. The form contains a file input type. The jQuery UI dialog contains an Upload button. When this button is clicked, I would like to programmatically call the submit method. My question is how I could select the form which is in a iframe using jQuery. The following code should clarify the picture:

<div id="upload_file_picker_dlg" title="Upload file">        
    <iframe id="upload_file_iframe" src="/frame_src_url" frameborder=0 width=100% scrolling=no></iframe>
</div>

frame_src_url contains:开发者_JAVA百科

<form action="/UploadTaxTable" enctype="multipart/form-data" method="post" id="upload-form">            
<p>Select a file to be uploaded:</p>
<p>
    <input type="file" name="datafile" size="60">
</p>

The jQueryUI dialog box javascript code:

$('#upload_file_picker_dlg').dialog({
    ...
    buttons: {
        'Close': function() { 
            $(this).dialog('close'); 
        },
        'Upload': function() {              
            $('#upload-form').submit(); //question is related to this line
            $(this).dialog('close'); 
        }
    },
    ....
});

From the javascript code snippet above, how can I select the form with id upload-form that is in the iframe whose id is upload_file_iframe ?


Accessing an element inside an iframe is tricky. You should use the following syntax:

$('#iframeID').contents().find('#upload-form').submit();

where 'iframeID' is obviously an ID you've given to the iframe.

Hope it is correct!


The answer is:

$('#upload_file_iframe').contents().find('#upload-form').submit();

which I learned from http://simple.procoding.net/2008/03/21/how-to-access-iframe-in-jquery/


Off the top of my head I'd say you might have to add the logic to the file where the iframe source is from.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜