首页 文章

Ajax表单提交输入类型文件

提问于
浏览
1

我使用ajax提交一个表单,使用PHP浏览,上传然后处理文件 . 我讨厌标准的html输入type =“file”按钮(就像大多数人一样)所以使用了一个技巧来点击我最终可以自定义的另一个按钮(id =“fileSelect”)的标准“浏览”按钮 . 在safari和其他人工作得很好,但使用IE提交表单的ajax代码行不会触发 . 执行ajax代码的其他元素(我得到警告(“Ajax完成”);最后),但是“$(”#imageform“) . ajaxForm({target:'#preview'}) . submit( );”行在IE中不起作用 .

Ajax代码:

$(document).ready(function() 
{       
    $('#xfile').live('change', function()   // when there is an even on the 'file' element (like file selected)
    { 
        $("#preview").html('');  // clears the preview area
        $("#preview").html('<img src="general_images/loading.gif" alt="Uploading...."/>');  // displays the loading
        $("#imageform").ajaxForm({target: '#preview'}).submit();
        alert ("Ajax complete");
    });
});

和HTML代码:

<div>
<button id="fileSelect" onclick="document.getElementById('xfile').click();">Insert Picture</button>
<form style="display: inline" id="imageform" name="imageform" action="upload_file_journal.php" method="post" target="_blank" enctype="multipart/form-data">
    <input style="visibility: hidden" type='file' name='xfile' id='xfile' /
</form>

1 回答

  • 0

    这在IE 9及更早版本中不起作用(我认为它适用于IE 10,11) .

    唯一真正的选择是使用绝对位置将上传控件置于按钮顶部,然后将不透明度设置为0.1,然后它们看起来像是单击按钮,但实际上是单击输入,您必须超大文件输入以确保它们始终单击按钮部分 .

    即使这样做也很麻烦 . 希望这可以帮助

相关问题