【问题标题】:asp.net dropdownlist onchange using jquery for the rendered htmlasp.net dropdownlist onchange 使用 jquery 为呈现的 html
【发布时间】:2012-01-06 00:00:46
【问题描述】:
    <asp:DropDownList runat="server" id="DropDownList1" 
DataSourceID="spdatasource1" DataValueField="CategoryName" 
AutoPostBack="false">
     </asp:DropDownList>  

这个 asp.net dropdonwlist 控件呈现以下 html。它被数据绑定到一个共享点列表。下拉选项正在动态填充。我正在尝试操纵一个选项的 onselect 以重定向到这样的某个链接:它确实被重定向到该站点,但它始终传递第一个选项值。如果我选择 Cancer,它仍然是 http://somesite/events/Pages/default1.aspx?cat=Select Category.. 为什么??

<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>

jquery用来抓取选项并传递给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
            });

【问题讨论】:

    标签: jquery redirect drop-down-menu onchange


    【解决方案1】:

    因为您在将函数绑定到选择下拉列表的更改事件的块之前设置了 selectedOption 的值。

    代码运行时。 $("#ctl00_PlaceHolderMain_ctl00_DropDownList1 option:selected").val() 的值为“选定类别”。然后在您的函数中使用该值。

    您的函数需要在对象更改时从对象中获取值,方法是使用$(this) 在函数内获取对该对象的引用。

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

    【讨论】:

      猜你喜欢
      • 2010-11-01
      • 1970-01-01
      • 2013-03-02
      • 2013-05-25
      • 2012-09-14
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多