使用jQuery选择iframe中的表单

Kev*_*nle 6 forms iframe jquery-ui

我在iframe中有一个表单,它位于jQuery UI对话框中.表单包含文件输入类型.jQuery UI对话框包含一个Upload按钮.单击此按钮时,我想以编程方式调用submit方法.我的问题是如何使用jQuery选择iframe中的表单.以下代码应阐明图片:

<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>
Run Code Online (Sandbox Code Playgroud)

frame_src_url包含:

<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>
Run Code Online (Sandbox Code Playgroud)

jQueryUI对话框的javascript代码:

$('#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'); 
        }
    },
    ....
});
Run Code Online (Sandbox Code Playgroud)

从上面的javascript代码段中,如何选择id为upload upload-form的表单,该表单位于iframe为upload_file_iframe的iframe中?

小智 16

访问iframe中的元素很棘手.您应该使用以下语法:

$('#iframeID').contents().find('#upload-form').submit();
Run Code Online (Sandbox Code Playgroud)

其中'iframeID'显然是您为iframe提供的ID.

希望这是对的!