无法从后面的aspx代码访问select值

时间:2014-07-03 13:42:48

标签: jquery asp.net html5

我必须从后面的aspx代码中获取html选择控件值...到目前为止我已完成以下编码:

以下代码段是填充下拉列表的jquery:

<script type="text/javascript">
        $(document).ready(function () {       // load jQuery 1.5
            function loadfail() {
                alert("Error: Failed to read file!");
            }

            function parse(document) {
                $(document).find("combo").each(function () {
                    var optionLabel = $(this).find('text').text();
                    var optionValue = $(this).find('value').text();
                    $('#combo1').append(
                   '<option value="' + optionValue + '">' + optionLabel + '</option>'
                    );
                });
            }

            $.ajax({
                url: 'dropdown.xml',    // name of file with our data
                dataType: 'xml',    // type of file we will be reading
                success: parse,     // name of function to call when done reading file
                error: loadfail     // name of function to call when failed to read
            });
        });

    </script>

以下代码段是html选择声明:

<select id="combo1" ></select>

任何人都可以帮助我如何获取aspx代码中下拉列表的选定值。

此致

2 个答案:

答案 0 :(得分:1)

您的下拉列表应添加runat = server

<select id="combo1" runat = "server"></select>

并且您的脚本需要更改,因为您已将html控件更改为服务器控件。这样你的脚本会是这样的,

function parse(document) {
                $(document).find($("#<%=combo1.ClientID %>")).each(function () {
                    var optionLabel = $(this).find('text').text();
                    var optionValue = $(this).find('value').text();
                    $('#').append(
                   '<option value="' + optionValue + '">' + optionLabel + '</option>'
                    );
                });
            }

答案 1 :(得分:0)

相反,我使用了以下技巧

在.aspx页面中我使用了这个

<select id="combo1" name="a">

在.aspx.cs页面中我使用了这个

string strValue = Page.Request.Form["a"].ToString();

这给了我索引号和休息排序我会做...