Not send GET variable on submit if dropwdown not s

2019-03-04 16:09发布

I have a form that I used to filter search results that consist of only dropdowns. I use GET rather then post so that the results can easily be shared with the URL.

    <form action="" name='filter' method="GET">
      <select name="Make" id="Make">
          <option selected="selected" value ="nothing">All</option>             
          <option  value="Toyota">Toyota</option>           
          <option value="Honda">Honda</option>
      </select>
      <input type="submit" value="Filter">
    </form>

As it is right now if It will submit the value "nothing" for the get variable Make if the user doesn't change the selection. They're are multiple drop downs identical as this one for model year etc.

Is it possible for the Make variable to not show up in the URL if it isn't used?

As it is now if that code is submited it will say website.com/?Make=nothing. I tried removing the value and then it says website.com/?Make=All. I do not want make to show up in the URL if "All" is selected. Is this possible?

1条回答
祖国的老花朵
2楼-- · 2019-03-04 16:30

You don't have a submit button :)
you can add a JS that runs on submit and checks the value of "Make" and in case it's "nothing" just do a simple redirect instead of submitting the form. Something like:

var e = document.getElementById("Make");
var val = e.options[e.selectedIndex].value;
if (val === 'nothing'){
  window.location = "http://www.google.com/"
}
查看更多
登录 后发表回答