html代码:
<form name="formupload" id="formupload" method="post" enctype="multipart/form-data" action="ashx/upload.ashx" >
<input type="file" name="inputfile"/>
<input type="submit" id="btnUpload" value="Press" />
<span id="spmsg"></span>
</form>
上面的代码如果去掉form标签,用JQuery写,该怎么写?
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$("#btnUpload").click(function() {
$.ajax({
type: "get",
dataType: "html",
url: "ashx/upload.ashx",
success: function(data) {
$("#spmsg").html(data);
}
});
})
})
</script>
这时异步传过去的data是什么啊?
该怎么写?
<form name="formupload" id="formupload" method="post" enctype="multipart/form-data" action="ashx/upload.ashx" >
<input type="file" name="inputfile"/>
<input type="submit" id="btnUpload" value="Press" />
<span id="spmsg"></span>
</form>
上面的代码如果去掉form标签,用JQuery写,该怎么写?
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$("#btnUpload").click(function() {
$.ajax({
type: "get",
dataType: "html",
url: "ashx/upload.ashx",
success: function(data) {
$("#spmsg").html(data);
}
});
})
})
</script>
这时异步传过去的data是什么啊?
该怎么写?
我说的是
$.ajax({
type: "get",
dataType: "html",
url: "ashx/upload.ashx",
data:
success: function(data) {
$("#spmsg").html(data);
}
});
这个里面的data
data:"id="+id+"&name="+name这样
示例:
function ajaxFileUpload() {
$("#imgloading")
.ajaxStart(function() {
$(this).show();
})
.ajaxComplete(function() {
$(this).hide();
});
;
$.ajaxFileUpload
(
{
url: '/ashx/upload.ashx',
secureuri: false,
fileElementId: 'inputfile',
dataType: 'json',
success: function(data, status)//上传成功后操作
{
if (typeof (data.error) != 'undefined') {
if (data.error != '') {
$("#spmsg").html(data.error);
}
}
else if (typeof (data.msg) != 'undefined') {
if (data.msg != '') {
window.location.href = data.msg;
}
}
},
error: function(data, status, e) //有错误时进行操作
{
$("#spmsg").html(e);
}
}
)
return false;
}
然后表单上的数据就可以这样取到
data:$("#defaultForm").formSerialize(),