简体   繁体   English

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

[英]function to disable/enable dynamically generated form fields

I'm no javascript expert and i'm currently trying to create a function for a form that has the same fields repeated depending on a number selected on a previous page. 我不是javascript专家,我正在尝试为具有相同字段的表单创建一个函数,具体取决于在前一页上选择的数字。

There could be between 1 and 10 rows of the form fields with each having a radio button selection that will enable/disable each row. 表单字段可以有1到10行,每行都有一个单选按钮选项,可以启用/禁用每一行。

At the moment i've written something but having trouble with concatenating form field names and variable names. 目前我已经写了一些东西但是在连接表单字段名和变量名时遇到了麻烦。

Is anyone able to point me in the right direction please. 有人能够指出我正确的方向吗?

Javascript: 使用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;
            }
        }
    }

Thanks for any help. 谢谢你的帮助。

J. J.

EDITED EDITED

Example of generated form HTML. 生成的表单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" />

You are mixing .NET code and JavaScript code. 您正在混合.NET代码和JavaScript代码。 Because .NET runs first, it will try to process the code as you have written it: 因为.NET首先运行,它会尝试在编写代码时处理代码:

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

and most likely generate an error message because that is invalid code. 并且很可能生成错误消息,因为这是无效的代码。

A simpler solution might be to use a class name. 更简单的解决方案可能是使用类名。 Then with jQuery, you could condense all of your code into a single call: 然后使用jQuery,您可以将所有代码压缩为一个调用:

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

Illegal javascript syntax. 非法的javascript语法。 You ARE mixing .net and JS 你正在混合.net和JS
var frmSchoolSelected+i is not allowed. var frmSchoolSelected+i不被允许。
Also your loop is assigning i instead of testing i (= versus ==) 你的循环也分配i而不是测试i(= vs ==)

try this 尝试这个

 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;
  }
}

A few notes on your approach. 关于你的方法的几点说明。

  • Be aware of how you're using this as it means different things in different contexts. 请注意您如何使用this因为它意味着不同环境中的不同事物。 In your case it would be better to pass in the index of the row you're toggling. 在你的情况下,传递你正在切换的行的索引会更好。 Your server side code most likely knows what row it's currently generating so this should be easy to accomplish. 您的服务器端代码很可能知道它当前正在生成哪一行,因此这应该很容易实现。
  • As others pointed out, you are mixing client side and server side. 正如其他人指出的那样,您正在混合客户端和服务器端。 In this case i is a client side variable that you're trying to use in a '<%=c_'... which is a server side context 在这种情况下, i是一个客户端变量,您尝试在'<%=c_'...它,这是一个服务器端上下文
  • I'm not quite sure why you're putting a + into what should be a variable name, but using a plus sign as part of a variable name isn't legal in JavaScript 我不太确定你为什么要把+变成一个变量名,但在JavaScript中使用加号作为变量名的一部分是不合法的
  • switchElement in this case isn't a CheckboxList as you're expecting it to be, it's just an html span element and as such won't have a meaningful value property. 在这种情况下, switchElement不是一个CheckboxList正如你所期望的那样,它只是一个html span元素,因此没有有意义的value属性。 You have to look at the actual input elements inside it and see if the yes element is checked (for example). 您必须查看其中的实际输入元素,并查看是否选中了yes元素(例如)。

If you were to go with a JavaScript solution you would need code along these lines 如果您使用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;
    }

If you were to approach this from jQuery side I would suggest making a few changes to your HTML as well. 如果您要从jQuery方面进行此处理,我建议您对HTML进行一些更改。 Your output can be thought of as a list of mini-forms so instead of having one large table with different rows corresponding to different parts, create a list (or a table with a single column if you aren't ready to give up on table based layout quite yet). 您的输出可以被认为是一个迷你表单列表,所以不要让一个大表与不同的部分对应不同的部分,而是创建一个列表(如果你还没有准备放弃表,就创建一个列表基于布局尚未完成)。

New 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>

New code 新代码

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");
    }
}

There are two nice things about this approach: 这种方法有两个好处:

  1. You are no longer relying on knowing what the ClientID will be as you're dealing with input elements as input elements 当您将输入元素作为输入元素处理时,您不再依赖于知道ClientID将是什么
  2. You can now refactor this input form into some sort of a repeating control (like a ListView ) so if you decide you'd like to change how each row is formatted, it'll be very easy to do (since it'll all be in one place). 您现在可以将此输入表单重构为某种重复控件(如ListView ),因此如果您决定要更改每行的格式,那么它将非常容易(因为它将全部是在一个地方)。

I got there eventually, once I had worked out how to add the onclick attribute to the input tag instead of the span tag I could then concentrate on the javascript function. 我最终到达那里,一旦我找到了如何将onclick属性添加到输入标签而不是span标签,我就可以专注于javascript函数。

Code behind Adds onclick to input tag. 代码隐藏添加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"

Generated HTML form 生成的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 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;
    }
}

Thanks to those who pointed me in the right direction, much appreciated. 感谢那些指出我正确方向的人,非常感谢。

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

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