admin管理员组文章数量:1290934
I am using a bination of Javascript and jQuery to create a FormData
object to send form data via AJAX request. This was my original code:
// $form is a jQuery object of the form in question
var formData = new FormData($form[0]);
My problem is finding a concise way to exclude invisible (i.e. .not(':visible')
) elements from the data collected by the FormData
object. I know that I could easily do this using jQuery's serialize()
method, but I need to use FormData
in this particular instance due to the upload of image files.
The only way I have managed to make this work is with the following code:
// $form is a jQuery object of the form in question
// Clone the original form
var $formClone = $form.clone();
// Remove invisible items from form
$form.find('input, textarea').not(':visible').remove();
// Collect form data with invisible items removed
var formData = new FormData($form[0]);
// Replace form with cloned form which retains invisible elements
$form.replaceWith($formClone);
I can't simply remove invisible elements from the clone and pass that to FormData, as none of the clone is visible unless I attach it to the DOM so all data elements are removed. So, my only solution was to clone the original form, and reserve the clone to re-attach to the DOM after removal of invisible elements from the original form.
I worry that using this method isn't very efficient. Is there a simpler way of achieving this?
I am using a bination of Javascript and jQuery to create a FormData
object to send form data via AJAX request. This was my original code:
// $form is a jQuery object of the form in question
var formData = new FormData($form[0]);
My problem is finding a concise way to exclude invisible (i.e. .not(':visible')
) elements from the data collected by the FormData
object. I know that I could easily do this using jQuery's serialize()
method, but I need to use FormData
in this particular instance due to the upload of image files.
The only way I have managed to make this work is with the following code:
// $form is a jQuery object of the form in question
// Clone the original form
var $formClone = $form.clone();
// Remove invisible items from form
$form.find('input, textarea').not(':visible').remove();
// Collect form data with invisible items removed
var formData = new FormData($form[0]);
// Replace form with cloned form which retains invisible elements
$form.replaceWith($formClone);
I can't simply remove invisible elements from the clone and pass that to FormData, as none of the clone is visible unless I attach it to the DOM so all data elements are removed. So, my only solution was to clone the original form, and reserve the clone to re-attach to the DOM after removal of invisible elements from the original form.
I worry that using this method isn't very efficient. Is there a simpler way of achieving this?
Share Improve this question asked Oct 16, 2014 at 13:39 MaccathMaccath 3,9665 gold badges29 silver badges43 bronze badges 1-
6
You could probably just set the elements that aren't visible to be disabled (
$form.find('input, textarea').not(':visible').prop('disabled', true);
), which would save having to clone the form, remove elements, then replace it again. I'd be surprised ifFormData
included disabled input elements. – Anthony Grist Commented Oct 16, 2014 at 13:44
2 Answers
Reset to default 6As @anthonyGist has pointed out in the ments, set invisible elements to disabled:
$(':hidden').prop('disabled', true);
The FormData has a delete method. So, you can just iterate through the hidden/disabled fields from the form you used to create the FormData and delete the ones you don't want in the object:
// $form is a jQuery object of the form in question
var formData = new FormData($form[0]);
form.find(':hidden').each(function(i, field) {
var fname = $(field).attr('name');
if(fname) {
formData.delete(fname);
}
});
本文标签: javascriptExcluding invisible form elements from FormData objectStack Overflow
版权声明:本文标题:javascript - Excluding invisible form elements from FormData object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741504622a2382245.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论