FormData and checkboxes

前端 未结 2 837
攒了一身酷
攒了一身酷 2021-02-14 05:22

Currently when creating a FormData object, a checked checkbox is added with a value of \"on\", and an unchecked checkbox is not passed at all.

Do I have to

2条回答
  •  花落未央
    2021-02-14 06:09

    Try this:

    var checkbox = $("#myForm").find("input[type=checkbox]");
    $.each(checkbox, function(key, val) {
        formData.append($(val).attr('name'), this.is(':checked'))
    });
    

    It always adds the field to FormData with either a value of true when checked, or false when unchecked.

提交回复
热议问题