用于禁用/启用动态生成的表单字段的函数

时间:2011-05-09 15:12:28

标签: javascript asp.net forms

我不是javascript专家,我正在尝试为具有相同字段的表单创建一个函数,具体取决于在前一页上选择的数字。

可能有1到10行表单字段,每行都有一个单选按钮选项,可以启用/禁用每一行。

目前我已经写了一些东西但是在连接表单字段名和变量名时遇到了麻烦。

是否有人能够指出我正确的方向。

使用Javascript:

        var i = 1;
    var iChildren = 2; //could be any number - depends what user selected.

    function toggle(switchElement) {
        for (i = 1; i = iChildren; i++) {

            var frmSchoolSelected+i = document.getElementById('<%=c_' & i & '_selected.ClientID%>');
            var frmSchoolAge+i = document.getElementById('<%=c_' & i & '_type.ClientID%>');
            var frmSchoolType+i = document.getElementById('<%=c_' & i & '_type1.ClientID%>');
            var frmSchoolAdditional+i = document.getElementById('<%=c_' & i & '_additional.ClientID%>');

            if (switchElement.value == 'Yes') {
                frmSchoolSelected+i.disabled = false;
                frmSchoolAge+i.disabled = true;
                frmSchoolType+i.disabled = true;
                frmSchoolAdditional+i.disabled = true;
            }
            else {
                frmSchoolSelected+i.disabled = true;
                frmSchoolAge+i.disabled = false;
                frmSchoolType+i.disabled = false;
                frmSchoolAdditional+i.disabled = false;
            }
        }
    }

感谢您的帮助。

学家

EDITED

生成表单HTML的示例。

<form method="post" action="schoolingform.aspx" onkeypress="javascript:return WebForm_FireDefaultButton(event, 'Button1')" id="form1">

    <table id="Table1" cellspacing="0" cellpadding="0" style="border-width:0px;border-collapse:collapse;">
        <tr>
            <td><strong>School Selected</strong></td>
            <td colspan="4"><span id="c_1_school_selected" onlick="javascript:toggle(this);">
                <input id="c_1_school_selected_0" type="radio" name="c_1_school_selected" value="Yes" />
                <label for="c_1_school_selected_0">Yes</label>
                <input id="c_1_school_selected_1" type="radio" name="c_1_school_selected" value="No" />
                <label for="c_1_school_selected_1">No</label>
                </span></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <th>Child</th>
            <th style="border-right:1px solid #dddddd;">School Name</th>
            <th>School Type</th>
            <th>School Type</th>
            <th>Additional Information</th>
        </tr>
        <tr valign="top">
            <td><strong>Fred Wilkinson</strong></td>
            <td style="border-right:1px solid #dddddd;"><input name="c_1_selected" type="text" id="c_1_selected" disabled="disabled" class="aspNetDisabled" style="width:190px;" />
                <input type="hidden" name="c_1_id" id="c_1_id" value="22" /></td>
            <td><select name="c_1_type" id="c_1_type" disabled="disabled" class="aspNetDisabled">
                    <option selected="selected" value="Primary">Primary</option>
                    <option value="Secondary">Secondary</option>
                    <option value="Higher Education">Higher Education</option>
                </select></td>
            <td><select name="c_1_type1" id="c_1_type1" disabled="disabled" class="aspNetDisabled">
                    <option selected="selected" value="State">State</option>
                    <option value="Independent">Independent</option>
                </select></td>
            <td><textarea name="c_1_additional" rows="6" cols="30" id="c_1_additional" disabled="disabled" class="aspNetDisabled" style="width:190px;"></textarea></td>
        </tr>
        <tr>
            <td><strong>School Selected</strong></td>
            <td colspan="4"><span id="c_2_school_selected" onlick="javascript:toggle(this);">
                <input id="c_2_school_selected_0" type="radio" name="c_2_school_selected" value="Yes" />
                <label for="c_2_school_selected_0">Yes</label>
                <input id="c_2_school_selected_1" type="radio" name="c_2_school_selected" value="No" />
                <label for="c_2_school_selected_1">No</label>
                </span></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <th>Child</th>
            <th style="border-right:1px solid #dddddd;">School Name</th>
            <th>School Type</th>
            <th>School Type</th>
            <th>Additional Information</th>
        </tr>
        <tr valign="top">
            <td><strong>Sara Wilkinson</strong></td>
            <td style="border-right:1px solid #dddddd;"><input name="c_2_selected" type="text" id="c_2_selected" disabled="disabled" class="aspNetDisabled" style="width:190px;" />
            <input type="hidden" name="c_2_id" id="c_2_id" value="23" /></td>
            <td><select name="c_2_type" id="c_2_type" disabled="disabled" class="aspNetDisabled">
                    <option selected="selected" value="Primary">Primary</option>
                    <option value="Secondary">Secondary</option>
                    <option value="Higher Education">Higher Education</option>
                </select></td>
            <td><select name="c_2_type1" id="c_2_type1" disabled="disabled" class="aspNetDisabled">
                    <option selected="selected" value="State">State</option>
                    <option value="Independent">Independent</option>
                </select></td>
            <td><textarea name="c_2_additional" rows="6" cols="30" id="c_2_additional" disabled="disabled" class="aspNetDisabled" style="width:190px;"></textarea></td>
        </tr>
        <tr>
            <td align="right" colspan="5"></td>
        </tr>
    </table>
    <input type="hidden" name="iChild" id="iChild" value="2" />
    <input type="submit" name="Button1" value="Next" id="Button1" class="submitBtn" />

4 个答案:

答案 0 :(得分:1)

您正在混合使用.NET代码和JavaScript代码。因为.NET首先运行,所以它会在你编写代码时尝试处理代码:

<%=c_' & i & '_selected.ClientID%>

并且很可能生成错误消息,因为这是无效的代码。

更简单的解决方案可能是使用类名。然后使用jQuery,您可以将所有代码压缩为一个调用:

$('.ClassName').toggle();

答案 1 :(得分:1)

非法的javascript语法。你正在混合.net和JS
{}不允许var frmSchoolSelected+i 你的循环也是分配i而不是测试i(= vs ==)

试试这个

 function toggle(switchElement) {
   var clientId = '<%=c_1_selected.ClientID%>';
   var isYes = switchElement.value == 'Yes';
   for (var i=1; i==iChildren; i++) {
     var frmSchoolSelected = document.getElementById(clientId.replace('_1_selected','_'+i+'_selected'));
     var frmSchoolAge = document.getElementById(clientId.replace('_1_selected','_'+i+'_type'));
     var frmSchoolType = document.getElementById(clientId.replace('_1_selected','_'+i+'_type1'));
     var frmSchoolAdditional = document.getElementById(clientId.replace('_1_selected','_'+i+'_additional'));
     frmSchoolSelected.disabled = !isYes;
     frmSchoolAge.disabled = isYes;
     frmSchoolType.disabled = isYes;
     frmSchoolAdditional.disabled = isYes;
  }
}

答案 2 :(得分:0)

关于你的方法的几点说明。

  • 请注意您使用this的方式,因为它在不同的环境中意味着不同的东西。在你的情况下,传递你正在切换的行的索引会更好。您的服务器端代码很可能知道它当前正在生成哪一行,因此这应该很容易实现。
  • 正如其他人所指出的,您正在混合客户端和服务器端。在这种情况下,i是您尝试在'<%=c_'...中使用的客户端变量,这是服务器端上下文
  • 我不太确定你为什么要将+放入应该是变量名的内容,但在JavaScript中使用加号作为变量名的一部分是不合法的
  • 在这种情况下,
  • switchElement不是您所期望的CheckboxList,它只是一个html span元素,因此不会有意义value 1}}属性。您必须查看其中的实际输入元素,并查看是否选中了 yes 元素。(例如)。

如果您使用JavaScript解决方案,则需要沿着这些行编写代码

    function toggle(i) {

        var schoolSelected = document.getElementById('c_' + i + '_school_selected_0').checked;

        // client side names of variables will be predictable so to an extent you can get away with
        // hard-coding them.  Not the best practice, but it'd work
        var frmSchoolSelected = document.getElementById('c_' + i + '_selected');
        var frmSchoolAge = document.getElementById('c_' + i + '_type');
        var frmSchoolType = document.getElementById('c_' + i + '_type1');
        var frmSchoolAdditional = document.getElementById('c_' + i + '_additional');

        // JavaScript, like some other languages lets you chain assignments like this
        frmSchoolSelected.disabled =
        frmSchoolAge.disabled = 
        frmSchoolType.disabled =
        frmSchoolAdditional.disabled = !schoolSelected;
    }

如果您要从jQuery方面进行此操作,我建议您对HTML进行一些更改。您的输出可以被认为是一个迷你表单列表,所以不要让一个大表与不同的部分对应不同的部分,而是创建一个列表(如果你还没有准备放弃表,就创建一个列表基于布局还是。)

新HTML

<ul>
    <li class="school1">
        <!-- school information form goes here -->
        ...
        <span id="c_1_school_selected" class="toggle" onclick='toggle("school1")'>
        ...
    </li>
    <li class="school2">
        <!-- school information form goes here -->
        ...
        <span id="c_1_school_selected" class="toggle" onclick='toggle("school2")'>
        ...
    </li>
    ...
</ul>

新代码

function toggle(row) {

    var allInputs = $("#" + row + " :input")
                        .not(".toggle input:radio");

    var state = $(".toggle :checked").val();

    if (state == "Yes") {
        allInputs.removeAttr("disabled");
    } else {
        allInputs.attr("disabled", "disabled");
    }
}

这种方法有两个好处:

  1. 当您将输入元素作为输入元素处理时,您不再依赖于知道ClientID将会是什么
  2. 您现在可以将此输入表单重构为某种重复控件(如ListView),因此如果您决定要更改每行的格式,那么这样做很容易(因为它都在一个地方)。

答案 3 :(得分:0)

我最终到达那里,一旦我找到了如何将onclick属性添加到输入标签而不是span标签,我就可以专注于javascript函数。

代码隐藏 添加onclick到输入标签。

        Dim newRadioYes As New RadioButton
        newRadioYes.Text = "Yes"
        newRadioYes.ID = "c_" & childID & "_school_selected_0"
        newRadioYes.Attributes.Add("onclick", "javascript:toggle(this, " & childID & ");")
        newRadioYes.Attributes.Add("value", "Yes")
        newRadioYes.GroupName = "c_" & childID & "_school_selected"

        Dim newRadioNo As New RadioButton
        newRadioNo.Text = "No"
        newRadioNo.ID = "c_" & childID & "_school_selected_1"
        newRadioNo.Attributes.Add("onclick", "javascript:toggle(this, " & childID & ");")
        newRadioNo.Attributes.Add("value", "No")
        newRadioNo.GroupName = "c_" & childID & "_school_selected"

生成的HTML表单

<form method="post" action="schoolingform.aspx" onkeypress="javascript:return WebForm_FireDefaultButton(event, 'Button1')" id="form1">
<table id="Table1" cellspacing="0" cellpadding="0" style="border-width:0px;border-collapse:collapse;">
    <tr>
        <td><strong>School Selected</strong></td>
        <td colspan="4"><input id="c_1_school_selected_0" type="radio" name="c_1_school_selected" value="Yes" onclick="javascript:toggle(this, 1);" />
            <label for="c_1_school_selected_0">Yes</label>
            <input id="c_1_school_selected_1" type="radio" name="c_1_school_selected" value="No" onclick="javascript:toggle(this, 1);" />
            <label for="c_1_school_selected_1">No</label></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <th>Child</th>
        <th style="border-right:1px solid #dddddd;">School Name</th>
        <th>School Type</th>
        <th>School Type</th>
        <th>Additional Information</th>
    </tr>
    <tr valign="top">
        <td><strong>Fred Wilkinson</strong></td>
        <td style="border-right:1px solid #dddddd;"><input name="c_1_selected" type="text" id="c_1_selected" disabled="disabled" class="aspNetDisabled" style="width:190px;" />
            <input type="hidden" name="c_1_id" id="c_1_id" value="26" /></td>
        <td><select name="c_1_type" id="c_1_type" disabled="disabled" class="aspNetDisabled">
                <option selected="selected" value="Primary">Primary</option>
                <option value="Secondary">Secondary</option>
                <option value="Higher Education">Higher Education</option>
            </select></td>
        <td><select name="c_1_type1" id="c_1_type1" disabled="disabled" class="aspNetDisabled">
                <option selected="selected" value="State">State</option>
                <option value="Independent">Independent</option>
            </select></td>
        <td><textarea name="c_1_additional" rows="6" cols="30" id="c_1_additional" disabled="disabled" class="aspNetDisabled" style="width:190px;"></textarea></td>
    </tr>
    <tr>
        <td><strong>School Selected</strong></td>
        <td colspan="4"><input id="c_2_school_selected_0" type="radio" name="c_2_school_selected" value="Yes" onclick="javascript:toggle(this, 2);" />
            <label for="c_2_school_selected_0">Yes</label>
            <input id="c_2_school_selected_1" type="radio" name="c_2_school_selected" value="No" onclick="javascript:toggle(this, 2);" />
            <label for="c_2_school_selected_1">No</label></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <th>Child</th>
        <th style="border-right:1px solid #dddddd;">School Name</th>
        <th>School Type</th>
        <th>School Type</th>
        <th>Additional Information</th>
    </tr>
    <tr valign="top">
        <td><strong>Sara Wilkinson</strong></td>
        <td style="border-right:1px solid #dddddd;"><input name="c_2_selected" type="text" id="c_2_selected" disabled="disabled" class="aspNetDisabled" style="width:190px;" />
            <input type="hidden" name="c_2_id" id="c_2_id" value="27" /></td>
        <td><select name="c_2_type" id="c_2_type" disabled="disabled" class="aspNetDisabled">
                <option selected="selected" value="Primary">Primary</option>
                <option value="Secondary">Secondary</option>
                <option value="Higher Education">Higher Education</option>
            </select></td>
        <td><select name="c_2_type1" id="c_2_type1" disabled="disabled" class="aspNetDisabled">
                <option selected="selected" value="State">State</option>
                <option value="Independent">Independent</option>
            </select></td>
        <td><textarea name="c_2_additional" rows="6" cols="30" id="c_2_additional" disabled="disabled" class="aspNetDisabled" style="width:190px;"></textarea></td>
    </tr>
    <tr>
        <td align="right" colspan="5"></td>
    </tr>
</table>
<input type="hidden" name="iChild" id="iChild" value="2" />
<input type="submit" name="Button1" value="Next" id="Button1" class="submitBtn" />

Javascript功能

    function toggle(switchElement, childID) {

    var frmSelected = document.getElementsByName('c_' + childID + '_school_selected');

    var frmSchoolSelected = document.getElementById('c_' + childID + '_selected');
    var frmSchoolAge = document.getElementById('c_' + childID + '_type');
    var frmSchoolType = document.getElementById('c_' + childID + '_type1');
    var frmSchoolAdditional = document.getElementById('c_' + childID + '_additional');

    if (switchElement.value == 'Yes') {
        frmSchoolSelected.disabled = false;
        frmSchoolAge.disabled = true;
        frmSchoolType.disabled = true;
        frmSchoolAdditional.disabled = true;
    }
    else {
        frmSchoolSelected.disabled = true;
        frmSchoolAge.disabled = false;
        frmSchoolType.disabled = false;
        frmSchoolAdditional.disabled = false;
    }
}

感谢那些指引我正确方向的人,非常感谢。