简体   繁体   English

javascript-将查询字符串添加到url

[英]javascript - Add a query string to url

I'm having trouble redirecting the user after submitting a form. 提交表单后,我无法重定向用户。 The form serves to allow the user to filter out doctors based on location, rating, and specialty. 该表格用于允许用户根据位置,等级和专业筛选出医生。 Here is my code for the form and javascript function: 这是我的表单和javascript函数代码:

<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 I submit the form, all it does to the URL is put an empty query string: /doctors? 但是,当我提交表单时,对URL所做的所有操作都将放置一个空查询字符串: /doctors?

Does anyone know why this is happening? 有人知道为什么会这样吗? Any help is much appreciated. 任何帮助深表感谢。

When you click the submit button, two things happen: 当您单击提交按钮时,会发生两件事:

  1. Some JavaScript runs that tells the browser to navigate to a new URL 运行一些JavaScript,告诉浏览器导航到新URL
  2. The form submits, causing the browser to navigate to a new URL 表单提交,导致浏览器导航到新的URL

The form submission comes last, so it wins. 最后提交表单,因此获胜。 The JavaScript is, in effect, useless. 实际上,JavaScript是无用的。

There are two approaches you can take to solve this. 您可以采用两种方法来解决此问题。

  1. Prevent the form submission from occurring 防止表单提交
  2. Make the form submission construct the URL you want 使表单提交构造所需的URL

Option 2 is the simple approach here. 选项2是这里的简单方法。

  1. Remove the JS entirely. 完全删除JS。 It doesn't do anything useful. 它没有任何用处。
  2. Add name attributes to all of your form controls. name属性添加到所有窗体控件。
  3. Set the attribute action="/doctors" in the ` action="/doctors" in the `中设置属性action="/doctors" in the

The form will now use the name and values to construct the query string you were putting together manually. 表单现在将使用名称和值来构造您手动组合在一起的查询字符串。

you are selecting wrong id below for "specialty" in getElementById . 您在下面的getElementById中为“ specialty”选择了错误的ID。

Here is the corrected one: 这是更正的一个:

var specialty = document.getElementById('specialty');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM