1

I want to submit a form with ajax. I get data of from using FormData like this:

var data = new FormData($(this)[0]);

one of form's input is a color value in HSV format. I want to convert it to hex value. I have a jquery function to convert it, but I don't know how can I manipulate it's value in FormData.

another question is: How can I remove some fields (key, value) from FormData?

Dharman
  • 30,962
  • 25
  • 85
  • 135
fullOfQuestion
  • 575
  • 2
  • 7
  • 19

1 Answers1

5

To get values by field name, use get().

data.get('color');

If there are multiple values sharing the same field name, use getAll().

data.getAll('colors');

To replace values by field name, use set().

data.set('color', '#abcdef');

It's important to note that set() replaces while append() appends when the field name exists. From the docs:

The set() method of the FormData interface sets a new value for an existing key inside a FormData object, or adds the key/value if it does not already exist.

The difference between set() and FormData.append is that if the specified key does already exist, set() will overwrite all existing values with the new one, whereas FormData.append will append the new value onto the end of the existing set of values.

To delete values by field name, use delete().

data.delete('color');
OXiGEN
  • 2,041
  • 25
  • 19