简体   繁体   English

Javascript-如何遍历中继器中的控件并找到某些下拉列表的值

[英]Javascript - How can I loop through the controls in a repeater and find the values of some dropdown lists

I'm currently experiencing a problem trying to find the selected values of some dropdown lists in a repeater. 我目前在尝试在中继器中查找某些下拉列表的选定值时遇到问题。 When I click a button on my form, I want to see what values the user gave in the dropdowns before I go into a postback. 当我单击表单上的按钮时,我想查看用户在回发之前在下拉列表中提供的值。

My repeater looks like this: 我的中继器如下所示:

 <asp:Repeater ID="rptProducts" runat="server">
     <HeaderTemplate>
        <tr class="Header">
          <td>Eye
           </td>
          <td> Code
          </td>
        </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
         <td><asp:DropDownList ID="ddlEye" runat="server" onchange='addToEye(this)'>
             <asp:ListItem Text="--Select--" />
              <asp:ListItem Text="Right" />
              <asp:ListItem Text="Left" />
               </asp:DropDownList>
         </td>
         <td>
             <asp:TextBox ID="txtTariff" size="7" runat="server" AutoPostBack="true" OnTextChanged="txtTarrif_TextChanged"
                                Text='<%# DataBinder.Eval(Container.DataItem, "Tariff") %>' />
         </td>
     </tr>
   </ItemTemplate>
</asp:Repeater>

Now the repeater gets built with a default of two rows, so the user should choose a value for each dropdown. 现在,中继器的默认行号为两行,因此用户应为每个下拉列表选择一个值。 I am trying to do validation on this. 我正在尝试对此进行验证。 I have tried this: 我已经试过了:

var right = 0;
var left = 0;
var inputs = document.getElementById('<%=rptProducts.ClientID %>').getElementsByTagName("select");
    for (var i = 0; i < inputs.length; i++) {
      if (inputs[i].id.options[dropdown.selectedIndex].value == "Right") {
      right += 1;
      } else if (inputs[i].id.options[dropdown.selectedIndex].value == "Left") {
      left += 1;
   }
}

but when I debug in Firefox, I get a exception: 但是当我在Firefox中调试时,出现异常:

document.getElementById(...) is null document.getElementById(...)为null

I have tried to make a seprate script tag with the following: 我尝试使用以下命令制作单独的脚本标签:

<script type="text/javascript">
    $(document).ready(function () {
        var name = [];
        var input = $("#<%= rptProducts.ClientID %>").find("select").each(function (i) {
            name[i] = $(this).attr('id');
            //alert($(this).attr('id'));
            //alert($(this).attr('name'));
        });
    });
</script>

But nothing happens in this function. 但是此功能没有任何反应。 Is there a way for me to properly find the dropdown lists and loop through the values to effectively do validation? 有没有办法让我正确找到下拉列表并遍历值以有效地进行验证?

A Repeater does not add it's ID (ClientID) to the HTML, so you cannot use that in jQuery as a selector. 中继器不会将其ID (ClientID)添加到HTML,因此您不能在jQuery中将其用作选择器。 Wrap the Repeater with an element that has an ID and use that. 用具有ID的元素包装中继器并使用它。

<table id="rptProductsTable">
    <asp:Repeater ID="rptProducts" runat="server">
    </asp:Repeater>
</table>

var input = $("#rptProductsTable").find("select").each(function (i) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM