有这样的东西:
<form name="cform" id="cform" method="post" enctype="multipart/form-data" action="{$sitepath}/comment.php">
<input type="file" class="custom-file-upload" name="image" id="exampleFormControlFile1"/>
<i class="mdi mdi-camera-enhance"></i> {#add_picture#}
<input type="text" name="url_add" id="test_{$firstsub.revid}" class="form-control pull-right" placeholder="{#url_add#}" style="display:none;"/>
<textarea name="text1" rows="3" cols="50" class="form-control" id="thirdfield"></textarea><br /><br />
<input type="submit" class="btn btn-info" name="submit" value="{#reply#}" />
</form>
工作正常/图像正常上载comment.php。
下面是相同的表单,但使用了jquery Post。工作正常,但javascript看不到图像...
<form name="cform0" id="cform0" method="post" enctype="multipart/form-data">
<input type="file" class="custom-file-upload" name="image" id="exampleFormControlFile1"/>
<i class="mdi mdi-camera-enhance"></i> {#add_picture#}
<textarea name="text1" rows="3" cols="50" class="form-control"></textarea><br /><br />
<input type="submit" id="submit_comment" class="btn btn-primary" name="submit" value="{#comment#}">
</form>
<script>
$('#newswhite').on('click', '#submit_comment', function() {
var url = "{$sitepath}/comment.php";
$.ajax({
type: "POST",
url: url,
data: $("#cform0").serialize(),
success: function(data)
{
$('#newswhite').load(location.href + " .newswhite1212");
}
});
return false;
});
</script>
有什么方法可以在脚本中添加一些我的comment.php会看到的图像吗?
要用ajax发送文件,您应该使用FormData()
https://developer.mozilla.org/en-us/docs/web/api/formdata/formdata