I'm trying to achieve an effect similar to 37signals' ta-da list - I want my users to be able to check off items from a list just by checking a "done" box - in other words a form gets submitted to the server on checking the box. Does anyone know of a tutorial which covers something like this, or could point me in the right direction?
Thanks Rob
If you need to submit a form when a checkbox is checked or when it is unchecked like when you are using a switch, a good way is to create an hidden input. If you try to submit the checkbox argument if the checkbox is unchecked the form will not be submitted at all. Find below my solution.
The onchange event occurs when the value of an element has been changed. For radiobuttons and checkboxes, the onchange event occurs when the checked state has been changed.
prop() and is() method are the two way by which we can check whether a checkbox is checked in jQuery or not. prop(): This method provides an simple way to track down the status of checkboxes. It works well in every condition because every checkbox has checked property which specifies its checked or unchecked status.
its simple...
<input type="checkbox" onclick="this.form.submit();">
If I understand your question correctly:
You could accomplish this using jQuery and AJAX. In the first example I'm doing it without submitting the whole form, and only submitting the value of the checkbox:
jQuery("#myCheckbox").click(function() {
var $checkbox = jQuery(this);
var checkboxData = "checkboxvalue=" + $checkbox.val();
jQuery.ajax({
url: "http://some.url.here",
type: "POST",
data: checkboxData,
cache: false,
dataType: "json",
success: function(data) {
if(data["success"]) {
//do some other stuff if you have to
//this is based on the assumption that you're sending back
//JSON data that has a success property defined
}
}
});
});
Presumably you'd have something on the server-side that handles the post.
If you actually do want to submit a form, you can do the same thing as above, except you'd serialize the form data:
jQuery("#myCheckbox").click(function() {
var formData = jQuery("#formID").serialize();
jQuery.ajax({
url: "http://some.url.here",
type: "POST",
data: formData,
cache: false,
dataType: "json",
success: function(data) {
if(data["success"]) {
//do some other stuff if you have to
//this is based on the assumption that you're sending back
//JSON data that has a success property defined
}
}
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With