为什么 input type=file 的变化没有被 `form.serialzie()` 识别
Why the change in input type=file is not identified with `form.serialzie()`
我正在检查 form
中的值在提交更新之前是否已更改。 form
内的所有其他控件,如果更改,将被识别但
不是 input type=file
控件。下面是我正在尝试的示例,如果您尝试提交 form
有或没有上传文件,响应是 未更改 。为什么这种行为只有 input type=file
?为什么未识别 input type=file
中的更改?
var form_serialize = "";
$(function() {
form_serialize = $("#frmProfile").serialize();
})
$('.submit').on('click', function(e) {
e.preventDefault();
var isChanged = $("#frmProfile").serialize() == form_serialize ? false : true;
if (isChanged)
$('body').append('changed');
else
$('body').append('Not changed');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="frmProfile">
<input type="file" />
<input type="submit" class="submit" value="Submit" />
</form>
根据 serialize()
的文档
Data from file select elements is not serialized.
您可以使用 jQuery ajaxForm plugin 支持。
或者你可以使用FormData
对象,参考这些问题:Jquery/Ajax Form Submission (enctype="multipart/form-data" ). Why does 'contentType:False' cause undefined index in PHP? , Sending multipart/formdata with jQuery.ajax
我正在检查 form
中的值在提交更新之前是否已更改。 form
内的所有其他控件,如果更改,将被识别但
不是 input type=file
控件。下面是我正在尝试的示例,如果您尝试提交 form
有或没有上传文件,响应是 未更改 。为什么这种行为只有 input type=file
?为什么未识别 input type=file
中的更改?
var form_serialize = "";
$(function() {
form_serialize = $("#frmProfile").serialize();
})
$('.submit').on('click', function(e) {
e.preventDefault();
var isChanged = $("#frmProfile").serialize() == form_serialize ? false : true;
if (isChanged)
$('body').append('changed');
else
$('body').append('Not changed');
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="frmProfile">
<input type="file" />
<input type="submit" class="submit" value="Submit" />
</form>
根据 serialize()
的文档Data from file select elements is not serialized.
您可以使用 jQuery ajaxForm plugin 支持。
或者你可以使用FormData
对象,参考这些问题:Jquery/Ajax Form Submission (enctype="multipart/form-data" ). Why does 'contentType:False' cause undefined index in PHP? , Sending multipart/formdata with jQuery.ajax