Submit a form on checkbox click
I currently use the following code to submit a form and then insert the result in a div. The form is submitted by pressing a button:
<script type="text/javascript">
$(document).ready(function()
{
$('#create').live ('submit', function() {
$.ajax(
{
data: $(this).serialize(), // get the form data
type: $(this).attr('method'), // GET or POST
url: $(this).attr('action'), // the file to call
success: function(response)
{ // on success..
$('#created').html(response); // update the DIV
}
});
$('#url').val('');
return false; // cancel original event to prevent form submitting
});
$("#content").load("contentpage.php");
});
</script>
I would l开发者_如何学Cike to change this so that rather than submitting the form with the button, I submit it by clicking a checkbox. Is this possible? There are a lot of forms on the page (and several checkboxes). The one that would need to submit the form is called "submitcheckbox"
Any help would be great...
Ross
Something along
$('#submitcheckbox').click(function() {
if ($(this).is(':checked')) {
// submit form here with your $.ajax call.
}
}
$('#submitcheckbox').bind('change', function() {
document.getElementById('create').submit();
});
This will do the trick
Checkbox exposes a click
event so you would basically change:
$('#create').live ('submit', function()
into
$('#submitcheckbox').live ('click', function()
Be aware however, that this will happen with checking AND unchecking the checkbox.
you can try this,
$('#submitcheckbox').bind('click', function() {
document.getElementById('formid').submit();
});
Thanks.
To submit Form through Ajax
, try this
Include jquery.form.js
.
More detail about this plugin is here http://jquery.malsup.com/form/
And add this script:
$(':checkbox').click(function(){
$('form[name="form_name"]').ajaxSubmit();
});
精彩评论