1

我有一个 javascript 附加到 GridView 的两个单元格中的两个文本框,该 GridView 在键入时自动填充同一单元格中的另外两个文本框。

它适用于第一个文本框,但在第二个文本框中输入时出现“未捕获的类型错误:无法设置未定义的属性‘值’”错误。

这是我的javascript:

function Sync(obj, idx) {
    var cell = obj.parentNode;
    cell.parentNode.cells[cell.cellIndex + 2].getElementsByTagName("input")[idx].value = obj.value;
}

网格视图:

<asp:TemplateField HeaderText="Country Code">
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtCountryCode" runat="server" Text='<%# Eval("CountryCode") %>' OnKeyUp="javascript:Sync(this, 0);"></asp:TextBox>
                                    <br />
                                    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="txtCountryCode" ValidationExpression="\d{1,3}" EnableClientScript="false" 
                                        ErrorMessage="The Country Code must be 1-3 numeric digits!" runat="server"
                                        ForeColor="Red" Font-Size="Smaller">
                                    </asp:RegularExpressionValidator>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblCountryCode" runat="server" Text='<%# Eval("CountryCode") %>'></asp:Label>
                                </ItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtNewCountryCode" runat="server"></asp:TextBox>
                                    <br />
                                    <asp:RegularExpressionValidator ID="RegularExpressionValidator2" ControlToValidate="txtNewCountryCode" ValidationExpression="\d{1,3}" EnableClientScript="false" 
                                        ErrorMessage="The Country Code must be 1-3 numeric digits!" runat="server"
                                        ForeColor="Red" Font-Size="Smaller">
                                    </asp:RegularExpressionValidator>
                                </FooterTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Main Number">
                                <EditItemTemplate>
                                    <asp:TextBox ID="txtMainNumber" runat="server" Text='<%# Eval("MainNumber")%>' OnKeyUp="javascript:Sync(this, 1);"></asp:TextBox>
                                    <br />
                                    <asp:RegularExpressionValidator ID="RegularExpressionValidator3" ControlToValidate="txtMainNumber" ValidationExpression="\d{1,14}" EnableClientScript="false" 
                                        ErrorMessage="The Country Code must be 1-14 numeric digits!" runat="server"
                                        ForeColor="Red" Font-Size="Smaller">
                                    </asp:RegularExpressionValidator>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="lblMainNumber" runat="server" Text='<%# Eval("MainNumber")%>'></asp:Label>
                                </ItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtNewMainNumber" runat="server"></asp:TextBox>
                                    <br />
                                    <asp:RegularExpressionValidator ID="RegularExpressionValidator4" ControlToValidate="txtNewMainNumber" ValidationExpression="\d{1,14}" EnableClientScript="false" 
                                        ErrorMessage="The Country Code must be 1-14 numeric digits!" runat="server"
                                        ForeColor="Red" Font-Size="Smaller">
                                    </asp:RegularExpressionValidator>
                                </FooterTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Phone">
                                <ItemTemplate>
                                    <asp:Label ID="lblPhone" runat="server" Text='<%# Eval("Phone") %>' Enabled="false"></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    +
                                    <asp:TextBox ID="txtPhoneCountryCode" runat="server" Enabled="false" Text='<%# Eval("CountryCode") %>' Width="30px"></asp:TextBox>
                                    <asp:TextBox ID="txtPhoneMainNumber" runat="server" Enabled="false" Text='<%# Eval("MainNumber")%>' Width="100px"></asp:TextBox>
                                </EditItemTemplate>
                                <FooterTemplate>
                                    <asp:TextBox ID="txtNewPhone" runat="server" Enabled="false"></asp:TextBox>
                                </FooterTemplate>
                            </asp:TemplateField>
4

1 回答 1

1

FooterTemplate与您的文本框相关的txtMainNumber仅包含 1 个文本框txtNewMainNumber,但您尝试设置其不存在的第二个文本框,更改以下内容

OnKeyUp="javascript:Sync(this, 1);"

OnKeyUp="javascript:Sync(this, 0);"
于 2012-11-08T13:32:18.383 回答