【发布时间】:2011-12-16 02:05:29
【问题描述】:
我正在使用 jQuery 构建文件上传,但在尝试设置表单属性时遇到 jQuery 错误:
$(document).ready(function () {
$("#formsubmit").click(function () {
var iframe = $('<iframe name="postframe" id="postframe" class="hidden" src="about:none" />');
$('div#iframe').append(iframe);
$('#theuploadform').attr("action", "/ajax/user.asmx/Upload")
$('#theuploadform').attr("method", "post")
$('#theuploadform').attr("userfile", $('#userfile').val())
$('#theuploadform').attr("enctype", "multipart/form-data")
$('#theuploadform').attr("encoding", "multipart/form-data")
$('#theuploadform').attr("target", "postframe")
$('#theuploadform').submit();
//need to get contents of the iframe
$("#postframe").load(
function () {
iframeContents = $("iframe")[0].contentDocument.body.innerHTML;
$("div#textarea").html(iframeContents);
}
);
}
);
<div id="uploadform">
<form id="theuploadform" action="">
<input id="userfile" name="userfile" size="50" type="file" />
<input id="formsubmit" type="submit" value="Send File" />
</form>
</div>
<div id="iframe" style="width: 0px; height: 0px; display: none;">
</div>
<div id="textarea">
</div>
【问题讨论】:
-
这一行的目的是什么?
$('#theuploadform').attr("userfile", $('#userfile').val())由于安全原因,浏览器实际上无法访问文件输入的值,它会返回一些虚假路径。 -
你错了,你可以读取值,但不能改变它。
-
返回值是什么?我确实说过它会返回一个值,而不是文件的实际路径。
标签: javascript jquery