malekabbes
malekabbes

Reputation: 11

Redirect url according to selection option

i faced a problem while making my website , its supposed to redirect me to another index when i choose a specific option for exemple when i select "Membre bénevole" the page "reg-benevole.html" will be loaded . and when i select "Responsable association", the page "reg-assocation.html" will be loaded.

but when i tested it just loads reg-benevole.html everytime for both options ...

    `<div class="form-group">
        <select id="cat" style="width:80%;" class="form-control" name="option">
            <option value="Responsable association" onChange="catgValue =this.value;"> Responsable
                association </option>
            <option value="Membre bénevole" onChange="catgValue =this.value;">Membre bénevole
            </option>
        </select>
    </div>
    <div class="form-group">
        <input type="submit" id="s" class="btn btn-primary" value="Soumettre">
    </div>`

and the Jquery Code :

   $(document).on('click', '#s', function () {
if (catgValue = 'Membre bénevole') {
    window.location.href = "reg-benevole.html";
}
else if (catgValue = 'Responsable association') {
    window.location.href = "reg-assocation.html";
}

});

Upvotes: 1

Views: 37

Answers (3)

Will Silveira
Will Silveira

Reputation: 383

1st: You are using a single '=' to compare. You need to use '==' (or '==='), like:

2nd: You need to get the select value inside the submit input click not outside it

3rd: You may need e.preventDefault() if you use the input inside form OR change the input type to button

4th: For double check you can use .trim() to avoid white spaces

$(document).on('click', '#s', function (e) {
  e.preventDefault();
  var catgValue = $('#cat').val().trim(); 
  if (catgValue == 'Membre bénevole') {
    window.location.href = "reg-benevole.html";
  }
  else if (catgValue == 'Responsable association') {
    window.location.href = "reg-assocation.html";
  }
});

Upvotes: 1

malekabbes
malekabbes

Reputation: 11

Thanks for your answers ! i appreciate this too much i changed the code like this and nothing yet .. (btw thanks for reminding me about the "==" i was kinda drunk xDD and i used "click" cuz it belongs to the submit button not the select options

    $(document).on('click', '#s', function () {
if (catgValue == 'Membre bénevole') {
    window.location.href = "reg-benevole.html";
}
else if (catgValue == 'Responsable association') {
    window.location.href = "reg-assocation.html";
}

})

Upvotes: 0

imvain2
imvain2

Reputation: 15847

Not only are you setting with a single = sign, but it is best to do change not the click event for selects. You don't need the onchange event in the options.

$(document).on('change', '#s option', function () {
if (this.value == 'Membre bénevole') {
    window.location.href = "reg-benevole.html";
}
else if (this.value == 'Responsable association') {
    window.location.href = "reg-assocation.html";
}

});

Upvotes: 0

Related Questions