【问题标题】:Why the change in input type=file is not identified with `form.serialzie()`为什么输入类型 = 文件的更改没有用 `form.serialize()` 标识
【发布时间】:2016-05-16 08:00:08
【问题描述】:

在提交更新之前,我正在检查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.
  • @PranavCBalan 知道如何克服这个问题吗?
  • 您可以在这里使用 ajaxForm 提交插件:malsup.com/jquery/form,这可能对您有所帮助
  • 哦..好的..谢谢..你能把它作为答案发布吗? @PranavCBalan..

标签: javascript jquery form-serialize


【解决方案1】:

根据serialize()的文档

文件选择元素中的数据未序列化。

您可以使用支持的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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 2010-12-04
    • 1970-01-01
    • 2011-08-15
    • 2019-04-26
    • 1970-01-01
    • 2013-12-29
    相关资源
    最近更新 更多