Submit form when changing checkbox

Asked

Viewed 103 times

0

Where am I going wrong with my form? When changing checkbox it needs to submit the form

HTML

<form id="form_onoff" name="form_onoff" action="onoff.php" method="post">
<input type="checkbox" value="on" id="slideThree" name="slideThree" />
<label for="slideThree"></label>

JQUERY

$(function($) {
$('input[type=checkbox][name=slideThree]').change(function() {
    $("#form_onoff").submit(function() {
        $.post('inc/onoff.php?id=<? echo $_SESSION['ids']; ?>', {slideThree: slideThree}, function(resposta) {
        });
    });
});

});

Thanks in advance

2 answers

1


Try to check the status of checkbox within the event change() thus:

$('input[type="checkbox"][name="slideThree"]').change(function() {
    if ($(this).is(':checked')) {
        // Aqui vai o $.post();
    }
});

Edit:

I noticed now that you are using the $.post, that is, is performing a requisition AJAX within your Submit. I don’t think it’s right to do it like this.

I suggest you remove the $("#form_onoff").submit() of your jQuery.

  • So. I put the post the way q was. And when I click it gives a latch and gets some time thinking :(

  • 1

    If you only use the $.post() by logic must work. :)

  • It worked. Line of the post was wrong. Brawl

  • 1

    Needing, we are there! D

1

Experimente assim:

$('input[type=checkbox][name=slideThree]').on('click', function() {
  $("#form_onoff").attr("action", "inc/onoff.php?id=<? echo $_SESSION['ids']; ?>");
  $("#form_onoff").submit();
});
  • So, but I think without the post function it will change page will not?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.