使用jquery获取下拉值并将其作为查询字符串传递给URL

时间:2012-01-06 00:23:31

标签: jquery url drop-down-menu query-string

我试图抓取用户从选项中选择时选择的任何内容,并将其作为查询字符串传递给当前URL。我究竟做错了什么?它总是抓住第一个选项

var selectedOption = $("#ctl00_PlaceHolderMain_ctl00_DropDownList1 option:selected").val();

            $("#ctl00_PlaceHolderMain_ctl00_DropDownList1").change(function(e) {
                  window.location.href = 'http://somesite/events/Pages/default1.aspx?cat=' + selectedOption
            });   

这是我正在处理的HTML。

<select name="ctl00$PlaceHolderMain$ctl00$DropDownList1" id="ctl00_PlaceHolderMain_ctl00_DropDownList1">
            <option value="Select Category">Select Category</option>
            <option value="All Categories">All Categories</option>
            <option value="Cancer">Cancer</option>
            <option value="Health Lecture">Health Lecture</option>
            <option value="Heart Health">Heart Health</option>
    </select>

2 个答案:

答案 0 :(得分:2)

您需要在change()函数中获取值。现在它正在抓取所选功能并在更改后触发重定向,因此它不会检测到选择的变化。

Example

答案 1 :(得分:2)

将您的jQuery更改为:

$("#ctl00_PlaceHolderMain_ctl00_DropDownList1").change(function(e) {
window.location.href = 'http://somesite/events/Pages/default1.aspx?cat=' + $(this).val()
}); 
相关问题