document.getElementsByName在IE11中不起作用,但在IE8中运行良好?

时间:2015-07-28 14:57:11

标签: javascript jquery internet-explorer

在IE11中,document.getElementsByName未按预期工作。

var colRadioButtonSelected = document.getElementsByName("radioButtonSelected_" + containerCurrencyCode);

var colLabelNetDollarsDue = document.getElementsByName("labelNetDollarsDue_" + containerCurrencyCode);

var colLabelUSDAmount = document.getElementsByName("labelUSDAmount_" + containerCurrencyCode); 
            for(var x=0; x < colRadioButtonSelected.length; x++)
            {      
                colRadioButtonSelected[x].disabled = readOnly ? "disabled" : ""; 
                var textNetDollarsDue = colLabelNetDollarsDue[x].innerText;

调试了代码并看到了document.getElementsByName部分在IE版本中返回不同类型的值。

enter image description here

尝试过如下的包装,但对我没用。任何帮助?

function getElementsByNameWrapper(name) {
  a = new Array();

  for (var i = 0; i < document.getElementsByName(name).length; ++i) {
    a.push(document.getElementsByName(name)[i]);
  }

  return a;
}

已更改为

var textNetDollarsDue = colLabelNetDollarsDue[x].textContent ||
    colLabelNetDollarsDue[x].innerText;

错误看起来像

enter image description here

HTML标记如下所示..

<asp:Repeater ID="ReportRepeater" runat="server" OnItemDataBound="ReportRepeater_ItemDataBound">
                                            <ItemTemplate>
                                                <tr class="<%# OddEven("odd", "even") %>">
                                                    <td align="center"><input type="radio" id="radioButtonSelected_<%# ((ReportEntity)Container.DataItem).BaseCurrency %>" name="radioButtonSelected_<%# ((ReportEntity)Container.DataItem).BaseCurrency %>" disabled="disabled" style="visibility: <%# (string.IsNullOrEmpty(((ReportEntity)Container.DataItem).SecurityNumber) || string.IsNullOrEmpty(((ReportEntity)Container.DataItem).CusipNumber)) ? "hidden" : "" %>;" value="<%# ((ReportEntity)Container.DataItem).IncomeReportId %>" /></td>
                                                    <td align="center"><%# ((ReportEntity)Container.DataItem).IncomeStatus %></td>
                                                    <td align="left"><%# String.Format("{0:d}", ((ReportEntity)Container.DataItem).AllocationDate) %></td>
                                                    <td align="center"><%# ((ReportEntity)Container.DataItem).SedolNumber %></td>
                                                    <td align="center"><%# ((ReportEntity)Container.DataItem).IsinNumber %></td>
                                                    <td align="center"><%# ((ReportEntity)Container.DataItem).ReportEvent %></td>
                                                    <td align="center"><%# ((ReportEntity)Container.DataItem).ReferenceNumber %></td>
                                                    <td align="left"><%# String.Format("{0:d}", ((ReportEntity)Container.DataItem).RecDate) %></td>
                                                    <td align="left"><label id="labelPayDate_<%# ((ReportEntity)Container.DataItem).BaseCurrency %>"><%# String.Format("{0:d}", ((ReportEntity)Container.DataItem).PayDate) %></label></td>
                                                    <td align="right"><%# String.Format("{0:#,##}",((ReportEntity)Container.DataItem).Quantity) %></td>
                                                    <td align="right"><%#  ((ReportEntity)Container.DataItem).BaseRate %></td>
                                                    <td align="right"><label id="labelNetDollarsDue_<%# ((ReportEntity)Container.DataItem).BaseCurrency %>"><%# Formatting.FormatCurrency(((ReportEntity)Container.DataItem).NetDollarsDue).Replace("$","")%></label></td>
                                                    <td align="center"><label id="labelUSDAmount_<%# ((ReportEntity)Container.DataItem).BaseCurrency %>"></label></td>
                                                    <td align="center">&nbsp;</td>
                                                    <td style="width:120px" align="center"><a id="Details" runat="server" >Show Details</a></td>                                    
                                                </tr>  

1 个答案:

答案 0 :(得分:1)

您的问题与.getElementsByName()无关。问题是IE11支持标准.textContent而不是旧(非标准).innerText

您可以考虑以下任一版本:

 var textNetDollarsDue = colLabelNetDollarsDue[x].textContent ||
    colLabelNetDollarsDue[x].innerText;

编辑 - 以上是真实的(虽然IE11可能仍然理解.innerText)但是你发布了标记,现在很明显问题是你正在使用{ {1}} API,但您真的对“id”值感兴趣。您可以为这些标签使用类名而不是“id”;无论如何,标记对所有重复的“id”值都无效。