0

我有一个文本框,我需要在 javascript 函数中设置其值。我计算网格中选中的复选框的数量,然后将值分配给隐藏字段,该隐藏字段的值又分配给文本框。以下是JS函数。

function CountChkBx_tpm() {
        var gvTrNomList = document.getElementById("gvTrNomList");
        var numChecked = document.getElementById("hdn2");
        var frm = document.forms['gvTrNomList'];
        var flag = false;
        for (var i = 0; i < document.forms[0].length; i++) {
            if (document.forms[0].elements[i].id.indexOf('IsTPMSelected') != -1) {
                if (document.forms[0].elements[i].checked) {
                    numChecked = numChecked + 1;
                }
            }
        }

        if (numChecked > 0)
            document.getElementById('<%=txtTPMRecom.ClientID %>').Value = numChecked;
        else
            document.getElementById('<%=txtTPMRecom.ClientID %>').Value = '0';
    }

它在网格内的复选框的 OnClick 处被调用:

<ItemTemplate>
                                <asp:CheckBox type="checkbox" runat="server" ID="IsTPMSelected" onclick="CountChkBx_tpm()" />
                            </ItemTemplate>

相同的功能用于另一个带有另一个 JS 函数的复选框来为另一个文本框设置值..这绝对没问题!有什么问题!!

  function CountChkBx() {
        var gvTrNomList = document.getElementById("gvTrNomList");
        var numChecked = document.getElementById("hdn");
        var frm = document.forms['gvTrNomList'];
        var flag = false;
        for (var i = 0; i < document.forms[0].length; i++) {
            if (document.forms[0].elements[i].id.indexOf('IsPocSelected') != -1) {
                if (document.forms[0].elements[i].checked) {
                    numChecked = numChecked + 1;
                }
            }
        }

        if (numChecked > 0) {

            document.getElementById('<%=txtCounterConfirmation.ClientID %>').value = numChecked;

        }
        else {
            document.getElementById('<%=txtCounterConfirmation.ClientID %>').Value = '0';
        }
    }

这是用于以下复选框

             <ItemTemplate>
                                <asp:CheckBox type="checkbox" runat="server" ID="IsPocSelected" onclick="CountChkBx()" />
                            </ItemTemplate>
                        </asp:TemplateField>

文本框是:

        <td style="width: 100px">
                            <asp:TextBox ID="txtCounterConfirmation" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        </td>
                        <td style="width: 100px">
                            <asp:TextBox ID="txtTPMRecom" runat="server"></asp:TextBox>
                        </td>
4

1 回答 1

0

据我认为你需要使用

document.getElementById('<%=txtTPMRecom.ClientID %>').value

代替

document.getElementById('<%=txtTPMRecom.ClientID %>').Value

结束 javscript 代码

function CountChkBx_tpm() {
        var gvTrNomList = document.getElementById("gvTrNomList");
        var numChecked = document.getElementById("hdn2");
        var frm = document.forms['gvTrNomList'];
        var flag = false;
        for (var i = 0; i < document.forms[0].length; i++) {
            if (document.forms[0].elements[i].id.indexOf('IsTPMSelected') != -1) {
                if (document.forms[0].elements[i].checked) {
                    numChecked = numChecked + 1;
                }
            }
        }

        if (numChecked > 0)
            document.getElementById('<%=txtTPMRecom.ClientID %>').value = numChecked;
        else
            document.getElementById('<%=txtTPMRecom.ClientID %>').value = '0';
    }
于 2012-09-05T12:22:04.510 回答