-
Notifications
You must be signed in to change notification settings - Fork 352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[request] Include unchecked checkbox in result #33
Comments
@ikhsan017 this is unlikely to be implemented as this project is moving forward to meet the specs described for W3C HTML JSON form submission Unchecked checkboxes are not meant to be submitted to the server, but this is not really a big problem. Think of it this way: <!-- client side -->
<form>
Email: <input name="email" value="[email protected]">
<input type="checkbox" name="newsletter">Receive News Letter?
</form>
<script>
var xhr = $post("/update-profile", $("form").serializeObject());
// ...
</script> Then in // server side
app.post("/update-profile", function() {
var email = req.param("email");
var newsletter = Boolean(req.param("newsletter"));
// ...
}); If the In case this example is a little confusing, let's look at it one more way.
By simply casting the value as a Boolean, we get both states Boolean("on"); // true
Boolean(null); // false We now have a perfectly toggling checkbox without submitting the unchecked checkbox to the server Because this is such a popular request, I'm tempted to make it an option for this plugin. However, larger organizations such as W3C and jQuery are leaning on one another here. W3C says "don't submit unchecked checkboxes" and jQuery is saying "we use the standard W3C rules". This plugin depends on the output of jQuery.serializeArray so the scope of this plugin doesn't even know that unchecked checkboxes exist! This is not the first time I've already started working on my own serializer that would give this plugin some more flexibility in terms of user options. Things like I'll leave this open to see what other people have to say about it. I suspect most people are requesting the "submit unchecked checkboxes" because they don't realize that there's still a way to detect the value as Feel free to reply in this thread if you have any other questions or ideas ✌️ |
Yeah, I got it, as implementation on major browser also have same behavior. But in my case, I am saving the form data as json-encoded data and put it into some textarea and need a clear state of the checkbox. scanning it like using Create an optional parameter to parse unchecked checkbox should be fine too, and follow the standard by default. btw, I found it already implemented here https://github.com/marioizquierdo/jquery.serializeJSON , quite similar project to yours :) regards |
@ikhsan017 yup I'm aware of his project, but I disagree with some of the implementation details. Also, his checklist of how his project is "better" maybe have been true at the time he wrote it, but that's no longer the case. I'll keep you updated here with the any related progress. |
@macek Just like to add my use case to this discussion . We call serializeObject() on the form and then merge it into another object. So not have the false value for some properties is causing us headaches (doesn't update the other object because the property doesn't exist). Agreed it shouldn't be enabled by default but for the cases where you are using the object client side (rather then just submitting the form to the server), its really needed to get a complete representation of the object (we now have to go an manually insert the false value) Hope this makes sense. |
Ended up adding a extra method onto the FormSerializer to support this because trying to work around it ended up being a nightmare.
|
@markkemper1 you might want to make sure you're modifying the latest version 2.5.0 will allow var $uncheckedInputs = $('#my-form :checkbox:not(:checked)').clone().prop('checked', true);
var obj = $('#my-form :input').add($uncheckedInputs).serializeObject(); Sorry for the delay on adding this feature to the latest version. Hope this helps a little better. |
Not sure that will help as I need to set the value to false in the resulting js object. In your example the properties would end up with true (or the value of the value attribute) ?. I only set the value to true, so that serializeArray will include the key / value pairs. The code does the following:
|
@markkemper1, My bad, I kinda just glanced at your code and threw and example together. The idea in principle would still work though: add additional fields - whatever they might be - to be serialized along with your other form fields. Maybe this will help lead you to a more complete solution function mySerializeFunction($form) {
var $uncheckedInputs = $form.find(':checkbox:not(:checked)').clone().map(function() {
return $(this).prop('checked', true).val(false);
});
return $form.find(':input').add($uncheckedInputs).serializeObject();
}
var obj = mySerializeFunction($('#my-form')); Again, it's not tested, but you can construct the additional fields however you want. A modification of the core plugin should not be necessary. |
Thanks, that looks better but (just glancing the source) won't the value be On 16 July 2015 at 14:40, Paul Maček [email protected] wrote:
|
@markkemper1 version Serializing the other types (like Edit Looking at it closer, you're right, the value would be the string value It looks like the checkboxes have to be children of the selector (variable Anyway, if you must get the boolean value of your unchecked checkboxes, I guess your other solution might be the only way for right now. Sorry if I've wasted any of your time. |
@marioizquierdo, any comments on #33 (comment) ? |
It's true I wrote the "why it's better" a while ago and certain checks don't really differentiate Also, implementing unchecked checkboxes in jquery.serializeJSON was not easy. It complicated the interface, the implementation and the available use cases, but there was hight demand for it. Relying on |
Solution: const $target = $(target)
const data = new FormSerializer($, $target)
.addPairs($target.serializeArray())
// add unchecked value
.addPairs(
$target.find(':checkbox[name]:not(:checked)')
.toArray()
.map(checkbox => ({name: checkbox.name, value: false}))
)
.serialize() |
Hi @macek ,
It is possible to add feature to include unchecked checkbox into the object or json result, something like
regards
Ikhsan
The text was updated successfully, but these errors were encountered: