c# - Cant get dropdown selected value with javascript? - Stack Overflow

My drop down(populated with an objectDataSource):<asp:DropDownList runat=server ID="ddlUserType

My drop down(populated with an 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 function getting the selected value of the dropdown:

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

But this is not working pow just keeps on returning undefined? This may be because user type doesnt seem to get anything assigned to it as it stays as null? Does anybody know why this code is not working?

My drop down(populated with an 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 function getting the selected value of the dropdown:

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

But this is not working pow just keeps on returning undefined? This may be because user type doesnt seem to get anything assigned to it as it stays as null? Does anybody know why this code is not working?

Share Improve this question asked Jul 19, 2013 at 11:40 Srb1313711Srb1313711 2,0475 gold badges25 silver badges35 bronze badges 4
  • ddlUserTypes isn't the clientId. – gdoron Commented Jul 19, 2013 at 11:42
  • What do you mean? Im a javascript newbie – Srb1313711 Commented Jul 19, 2013 at 11:43
  • Read my answer below. – gdoron Commented Jul 19, 2013 at 11:44
  • I tried what you suggested but 'userType' was still null and 'pow' still undefined – Srb1313711 Commented Jul 19, 2013 at 11:49
Add a ment  | 

4 Answers 4

Reset to default 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.

Use this I hope it's working.

var e = document.getElementById("ddlUserTypes");
var strUser = e.options[e.selectedIndex].value;
var userType = documentgetElemebyById('<%= ddlUserTypes.ClientID %>');

You should read this

<asp:DropDownList CssClass="ddlClass" ...>

It will be transformed like

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745222391a4617308.html

相关推荐

  • c# - Cant get dropdown selected value with javascript? - Stack Overflow

    My drop down(populated with an objectDataSource):<asp:DropDownList runat=server ID="ddlUserType

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信