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:

  1. some javascript runs tells browser navigate new url
  2. 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.

  1. prevent form submission occurring
  2. make form submission construct url want

option 2 simple approach here.

  1. remove js entirely. doesn't useful.
  2. add name attributes of form controls.
  3. set attribute action="/doctors" in the`

the form use name , values construct query string putting manually.


Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -