html - javascript - Add a query string to url -
i'm having trouble redirecting user after submitting form. form serves allow user filter out doctors based on location, rating, , specialty. here code form , javascript function:
<form onsubmit="getdoctors()" id="searchform"> <div> <div class="col-sm-3"> <div class="form-group"> <label for="specialty">specialty:</label> <select class="form-control" id="specialty"> <option value="">select</option> <% specialtylist.foreach(function(specialtyvalue){%> <option value="<%= specialtyvalue %>"><%= specialtyvalue %></option> <% }); %> </select> </div> </div> <div class="col-sm-3"> <div class="form-group"> <label for="rating">min. rating:</label> <select class="form-control" id="rating"> <option value="">select</option> <option value="0">0</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> </div> </div> <div class="col-sm-3"> <div class="form-group"> <label for="location">location:</label> <select class="form-control" id="location"> <option value="">select</option> <% locationlist.foreach(function(locationvalue){%> <option value="<%= locationvalue %>"><%= locationvalue %></option> <% }); %> </select> </div> </div> <div class="col-sm-3"> <div class="form-group"> <input type="submit" class="btn btn-success" value="search"> </div> </div> </div> </form> function getdoctors() { var specialty = document.getelementbyid('speciality'); var rating = document.getelementbyid('rating'); var location = document.getelementbyid('location'); window.location.href("/doctors?specialty=" + specialty.options[specialty.selectedindex].text + "&rating=" + rating.options[rating.selectedindex].text + "&location=" + location.options[location.selectedindex].text); }
however, when submit form, url put empty query string: /doctors?
does know why happening? appreciated.
when click submit button, 2 things happen:
- some javascript runs tells browser navigate new url
- the form submits, causing browser navigate new url
the form submission comes last, wins. javascript is, in effect, useless.
there 2 approaches can take solve this.
- prevent form submission occurring
- make form submission construct url want
option 2 simple approach here.
- remove js entirely. doesn't useful.
- add
name
attributes of form controls. - set attribute
action="/doctors" in the
`
the form use name , values construct query string putting manually.
Comments
Post a Comment