Form action as radio button selection does not work

Asked

Viewed 55 times

1

I am doing a validation of 2 radios Buttons, where depending on which form is selected forwards to a different page. I am using Jquery as below:

jQuery(document).ready(function($) {
    var form = $('form[name="cadastro"]'),
        radio = $('input[name="MERCADO"]'),
        choice = '';

    radio.change(function(e) {
        choice = this.value;
        if (choice === "ME") {
           form.attr('action', 'Checklist-P1-ME.php');
        } else {
           form.attr('action', 'Checklist-P1.php');
        }
    });
});

The radios are these:

    <input type="radio" name="MERCADO" value="MI" CHECKED/>MERCADO INTERNO<br><br>
    <input type="radio" name="MERCADO" value="ME" />MERCADO EXTERNO<br><br><br>

But what happens is that Jquery only recognizes the MI value if I click on the ME and then click back on the MI. It’s like it doesn’t recognize the checked preset.

Any suggestions?

  • Why don’t you put default form action? depending on the input that starts checked? In this case the form action would start as Checklist-P1.php by default

  • I can’t believe I asked a question with such a simple solution.... I left the action default and it worked.... thanks

  • :P there is no problem in that, sometimes the obvious/simple solution passes us by

No answers

Browser other questions tagged

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