0

我的下拉列表(填充有objectDataSource):

<asp:DropDownList runat=server ID="ddlUserTypes" DataSourceID="odsUserTypes" AppendDataBoundItems=true DataTextField="UserType" DataValueField="Usertype">
<asp:ListItem Value="">-Please select one-</asp:ListItem>
</asp:DropDownList>

获取下拉列表的选定值的 Javascript 函数:

<script type="text/javascript" language="javascript">
    var notRegistered = false;
    var email = '';
    var userType = document.getElementById("ddlUserTypes");
    var pow = userType.options[userType.selectedIndex].value;
    function PreRegister() {debugger;
        if (notRegistered) {
            location.href = '/Register.aspx?pageid=<%= ConfigHelper.RegistrationPageId %>&Email=' + encodeURIComponent(email)+'&asd='+encodeURIComponent(pow);
            return false;
        }
        return true;
    }
</script>

但这不起作用pow只是继续返回未定义?这可能是因为用户类型似乎没有得到任何分配给它的东西,因为它保持为空?有谁知道为什么这段代码不起作用?

4

4 回答 4

2

As gdoron mentioned, ASP.Net will make the HTML id of your dropdown something other than what you set as the ID parameter to your <asp:DropDownList>, which is why your JS isn't finding it.

What may be easiest is to assign a class to your dropdown, and then in your JS, target the element by class.

于 2013-07-19T11:44:14.407 回答
1

Use this I hope it's working.

var e = document.getElementById("ddlUserTypes");
var strUser = e.options[e.selectedIndex].value;
于 2013-07-19T11:43:05.357 回答
1
var userType = documentgetElemebyById('<%= ddlUserTypes.ClientID %>');

You should read this

于 2013-07-19T11:43:46.883 回答
0
<asp:DropDownList CssClass="ddlClass" ...>

它会像

<select class="ddlClass" ...>
    <option value="value1">value1</option>
    <option selected="selected" value="value2">value2</option>
    ...
</select>
// use jQuery
var selected = $('.ddlClass').val();

// withour jQuery 
// method1
var selected1 = document.querySelector('.ddlClass option[selected="selected"]').value;
// method2
var selected2 = document.querySelector('.ddlClass option:checked').value;
于 2021-11-09T03:49:51.333 回答