0

I have a simple form with one select box and two options in it. Here is the related jQuery code:

 $('.myCssClass').on('change', function() {
       alert("This is executed");
       $(this).parent().submit(function(e) {
       alert("This is NOT executed");
          $.ajax({
                 type: "POST",
                 url: "script.php",
                 data: $(this).parent().serialize(), 
                 success: function(data)
                 {
                     alert(data);
                 }
               });
          e.preventDefault();
      });

Form is a parent of a select box. So the first alert is executed when I change the select box option, but then next one is never reached. Any idea why?

2
  • Next one will be executed when you submit the form by clicking on the submit button.
    – Tushar
    Commented Jun 28, 2016 at 9:53
  • @Tushar Okay, thanks. I don't have a submit button actually. I want to execute server script on select box change. What should I do ? Thanks for your time :)
    – Whirlwind
    Commented Jun 28, 2016 at 9:55

5 Answers 5

1

This should do it. Don't bother with the submit event - it will not be triggered by a select box.

 $('.myCssClass').on('change', function() {
      $.ajax({
             type: "POST",
             url: "script.php",
             data: $(this).parent().serialize(), 
             success: function(data)
             {
                 alert(data);
             }
      });
  });
1
  • Okay, I tried this one and it worked. Thanks a bunch! Will accept answer in few minutes (system won't allow me to do that earlier).
    – Whirlwind
    Commented Jun 28, 2016 at 10:01
1

You have to create the submit event listener outide the other event:

$('.myCssClass').parent().submit(function(e) {
    $.ajax({
        type: "POST",
        url: "script.php",
        data: $(this).serialize(), 
        success: function(data){
            alert(data);
        }
    });

    e.preventDefault();
});

$('.myCssClass').on('change', function() {
    $(this).parent().submit();
});

Or as chain:

$('.myCssClass').on('change', function() {
    $(this).parent().submit();
})
.parent().submit(function(e) {
    $.ajax({
        type: "POST",
        url: "script.php",
        data: $(this).serialize(), 
        success: function(data){
            alert(data);
        }
    });

    e.preventDefault();
});

But why two events? Just send the data on change:

$('.myCssClass').on('change', function() {
    $.ajax({
        type: "POST",
        url: "script.php",
        data: $(this).parent().serialize(), 
        success: function(data){
            alert(data);
        }
    });
});
1

Try the following:

$('.myCssClass').on('change', function() {
    alert("This is executed");
    $(".form-with-class").submit();
});

$(".form-with-class").on('submit', function(e){
  e.preventDefault();
  $.ajax({
      type: "POST",
      url: "script.php",
      data: $(".form-with-class").serialize(), 
      success: function(data){
        alert(data);
      }
  });
});
1
$('.myCssClass').on('change', function() {
    alert("This is executed");
    $(this).parent('form#id').submit();  // improve performance by reducing the traversal
});

$("#form-with-id").on('submit', function(e){
  e.preventDefault();
  var data = $(this).serialize();
  $.ajax({
      type: "POST",
      url: "script.php",
      data: data,
      success: function(data){
        alert(data);
      }
  });
});

Hope this works.

1
  • data: $(this).parent().serialize() should be data: $(this).serialize() I think - since you're handling this within a submit even of the form, then $(this) will be the form itself.
    – ADyson
    Commented Jun 28, 2016 at 9:58
1

when a handler is provided it just registers the handler but it does not perform the actual submit.

You may want to call:

$(this).parent().submit();

after your handler is registered. Furthermore, within your handler, you have to refer to the form just by "$(this)" (not: "$(this).parent()"), since the handler belongs to the form.

But since you'd call the submit explicit, there is no point in registering a handler which you then invoke. You could fire your ajax-request directly:

$('.myCssClass').on('change', function() {
   alert("This is executed");
      $.ajax({
             type: "POST",
             url: "script.php",
             data: $(this).parent().serialize(), 
             success: function(data)
             {
                 alert(data);
             }
           });
  });

Not the answer you're looking for? Browse other questions tagged or ask your own question.