mercredi 17 juin 2020

Redirect to another page button inside option tag always redirect to the same page (JavaScript)

I made a <select> tag with some option tags in it

<select name="Persons" onchange="changePage(this.value); this.selectedindex = -1">
<option class="op" value="0">option1 </option>
<option class="op" value="1">option2 </option>

and so on.. and using js I made an array of values values = ['0','1']

then made a function to redirect to a page according to the value clicked

function changePage(){
   for (let i=0; i < values.length ; i++){
    let value = values[i]
        if (value=1){
            window.location.replace("link1");
           } else if (value=2) {
            window.location.replace("link2");
           }
    }
}
}

whenever I click option 1 or 2 it redirect to option 1 only

expected : redirect according to value (if =1 redirect to link1 if =2 redirect to link2)

Aucun commentaire:

Enregistrer un commentaire