将下拉选择的值作为查询字符串传递给 URL

发布于 2024-12-23 05:41:17 字数 572 浏览 6 评论 0原文

在 HTML 下拉值的选择事件中,我想触发定向到某个 URL 并将所选值作为查询字符串附加到 URL 的操作。如何在 javascript 或 jquery 中实现这一点?

<select id="hospitalDropDown">
            <option>All Hospitals</option>
            <option>Dyer</option>
        <option>Carmel</option>
        <option>Indianapolis</option>
        <option>Beech Grove</option>
</select> 

因此,如果选择 Dyer,结果应定向到 http://mysite.com/events。 aspx?hosp=Dyer

有人可以帮忙吗?

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. How to achieve that in javascript or 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

Can someone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

强者自强 2024-12-30 05:41:17

在 jquery

$( '#hospitalDropDown' ).on( 'change', function( e ){
  document.location.href = "http://blah" + $( this ).val();
});

编辑:将“bind”更改为更现代的“on”

in jquery

$( '#hospitalDropDown' ).on( 'change', function( e ){
  document.location.href = "http://blah" + $( this ).val();
});

EDIT: changed "bind" to the more modern "on"

心在旅行 2024-12-30 05:41:17

如果您使用的是最新版本的 jQuery,则可以使用 on() 函数

$('#hospitalDropDown').on('change', function () {
    window.location.assign('http://mysite.com/events.aspx?hosp=' + $(this).val());
});

要查看实际效果:http://jsfiddle.net/73PEg/

If you're using the latest version of jQuery, you can attach an event handler using the on() function.

$('#hospitalDropDown').on('change', function () {
    window.location.assign('http://mysite.com/events.aspx?hosp=' + $(this).val());
});

To see this in action: http://jsfiddle.net/73PEg/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文