在任何下拉列表的更改事件中将所有下拉列表选定的值放入 url 参数中?
我的 mypage.php 中有三个下拉菜单:
<select name='vehicle'>
<option value=''>Select</option>
<option value='bus'>Bus</option>
<option value='bike'>Bike</option>
<option value='car'>Car</option>
</select>
<select name='color'>
<option value=''>Select</option>
<option value='red'>Red</option>
<option value='blue'>Blue</option>
<option value='green'>Green</option>
</select>
<select name='cities'>
<option value=''>Select</option>
<option value='newyork'>New York</option>
<option value='london'>London</option>
<option value='paris'>Paris</option>
</select>
当我第一次打开页面时,在所有下拉菜单中都会选择“选定”选项。
问题:
当我从车辆下拉列表中选择公交车时,URL应该是:
mypage.php?vehicle=bus
然后当我从颜色下拉列表中选择红色时,URL应该是:
mypage.php?vehicle=bus&color=red
然后当我从城市下拉列表中选择巴黎,则网址应为:
mypage.php?vehicle=bus&color=red&city=paris
然后我再次从车辆中选择汽车,则网址应为:
mypage.php?vehicle=car&color=red&city=paris
I have three drop downs in mypage.php:
<select name='vehicle'>
<option value=''>Select</option>
<option value='bus'>Bus</option>
<option value='bike'>Bike</option>
<option value='car'>Car</option>
</select>
<select name='color'>
<option value=''>Select</option>
<option value='red'>Red</option>
<option value='blue'>Blue</option>
<option value='green'>Green</option>
</select>
<select name='cities'>
<option value=''>Select</option>
<option value='newyork'>New York</option>
<option value='london'>London</option>
<option value='paris'>Paris</option>
</select>
When I open page first time then in all dropdown 'Selected' option is selected.
Question:
When I select Bus from vehicle dropdown then url should be:
mypage.php?vehicle=bus
Then when I select Red from color dropdown then url should be:
mypage.php?vehicle=bus&color=red
Then when I select Paris from cities dropdown then url should be:
mypage.php?vehicle=bus&color=red&city=paris
Then I again select Car from vehicle then url should be:
mypage.php?vehicle=car&color=red&city=paris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用特定的 js 框架吗?此行为是标准表单提交行为。您需要将表单作为 GET 请求提交,以便将参数放入实际的 URL 中,但正如我所说,任何表单提交都会默认实现此目的,因为这就是 GET 表单提交的工作方式。
例如,在 jQuery 中,您可以使用以下方法在选择更改时触发表单提交:
您的表单将类似于:
Are you using a particular js framework? This behaviour is the standard form submit behaviour. You need to submit the form as a GET request in order to put the params in the actual URL, but as I say, any form submit will achieve this by default as that's the way GET form submits work.
In jQuery for instance you can trigger a form submit on select change using:
You form would be something like:
像这样的东西(使用jquery使选择更容易)
上面的代码可能包含很多错误,但你应该能够明白这个想法。
Something like this (using jquery to make selection easier)
The above code probably contains lots of errors, but you should be able to get the idea.