Not send GET variable on submit if dropwdown not selected

旧城冷巷雨未停 提交于 2019-12-31 07:34: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:


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/"
}


来源:https://stackoverflow.com/questions/12412682/not-send-get-variable-on-submit-if-dropwdown-not-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!