Home > other >  jQuery prop() does not set checkbox to true
jQuery prop() does not set checkbox to true

Time:04-21

the following jQuery 3.2.1 line: $("#company-checkbox").attr('disabled', true).prop("checked", true);

intends to set a checkbox to true and lock it. Indeed visually this happens but in practice when form data are submitted, the checkbox is not submitted with true value. Is this due to attr('disabled', true)? How can I have the desired result?

CodePudding user response:

According to specs, disabled controls are not considered when preparing POST or query string data. If you absolutely have to send something, you can use jQuery to append a hidden input that has same name and value as the checkbox.

CodePudding user response:

$(elem).prop("checked") -> true (Boolean) Will change with checkbox state

$(elem).attr("checked")(1.6 ) -> "checked" (String) Initial state of the checkbox; does not change

Try using the 'prop' function only

$("#company-checkbox").prop("checked", true);

CodePudding user response:

Please check the input company-checkbox has the attribute "name" set.

  • Related