[英]Passing dropdown selected value as query string to URL
On select event of a HTML dropdown value, I want to trigger being directed to a certain URL and that selected value being appended to the URL as query string. 在HTML下拉值的select事件中,我想触发被定向到某个URL,然后将该选定值作为查询字符串附加到URL。 How to achieve that in javascript or jquery? 如何在javascript或jquery中实现呢?
<select id="hospitalDropDown">
<option>All Hospitals</option>
<option>Dyer</option>
<option>Carmel</option>
<option>Indianapolis</option>
<option>Beech Grove</option>
</select>
So if Dyer is selected, the result should be being directed to http://mysite.com/events.aspx?hosp=Dyer 因此,如果选择了Dyer,则应将结果定向到http://mysite.com/events.aspx?hosp=Dyer
Can someone help? 有人可以帮忙吗?
in jquery 在jQuery中
$( '#hospitalDropDown' ).on( 'change', function( e ){
document.location.href = "http://blah" + $( this ).val();
});
EDIT: changed "bind" to the more modern "on" 编辑:更改为更现代的“绑定”
If you're using the latest version of jQuery, you can attach an event handler using the on() function . 如果您使用的是最新版本的jQuery,则可以使用on()函数附加事件处理程序。
$('#hospitalDropDown').on('change', function () {
window.location.assign('http://mysite.com/events.aspx?hosp=' + $(this).val());
});
To see this in action: http://jsfiddle.net/73PEg/ 要查看实际效果: http : //jsfiddle.net/73PEg/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.