【问题标题】:Enable/Disable Controls on selection of Radio using jquery使用 jquery 启用/禁用对 Radio 选择的控制
【发布时间】:2016-07-14 03:31:04
【问题描述】:

我正在尝试根据收音机的选择启用/禁用某些控件。它似乎不起作用。 我希望在选择是单选时启用禁用的文本和单选字段。 请帮忙!

ASP sn-p

                <div class="form-group" >
                    <label class="control-label">Whether any other children of the same family is studying in Primary/ Secondary Section of SVVHS</label><br />
                    <input type="radio" value="yes" id="chkRelatedStudentYes" runat="server" name="RelatedStudent" required="required"/> <label class="control-label">Yes</label><br />
                    <input type="radio" value="no" id="chkRelatedStudentNo" runat="server" name="RelatedStudent" required="required"/> <label class="control-label">No</label>
                 </div>
                <div class="form-group">
                    <label class="control-label">Name of the Student</label>
                    <input maxlength="100" type="text" required="required" class="form-control" placeholder="Enter Name of the Related Student" id="txtRelatedStudentName" runat="server" disabled="disabled" />
                </div>
                <div class="form-group">
                    <label class="control-label">Section</label><br />
                    <input type="radio" value="Primary" id="chkPrimary" runat="server" name="Section"  required="required" disabled="disabled"/> <label class="control-label">Primary</label><br />
                    <input type="radio" value="Secondary" id="chkSecondary" runat="server" name="Section"  required="required" disabled="disabled"/> <label class="control-label">Secondary</label>
                </div>
                <div class="form-group">
                    <label class="control-label">Standard</label>
                    <input maxlength="12" type="text" required="required" class="form-control" placeholder="Enter Standard of the Related Student" id="txtStandard" runat="server" disabled="disabled"/>
                </div>
                <div class="form-group">
                    <label class="control-label">Division</label>
                    <input maxlength="12" type="text" required="required" class="form-control" placeholder="Enter Division of the Related Student" id="txtDivision" runat="server" disabled="disabled"/>
                </div>

jquery sn-p

        <script type="text/javascript">
            $('#chkRelatedStudentYes').click(function () {
                $('#txtRelatedStudentName').removeAttr("disabled");
                $('#chkPrimary').removeAttr("disabled");
                $('#chkSecondary').removeAttr("disabled");
                $('#txtStandard').removeAttr("disabled");
                $('#txtDivision').removeAttr("disabled");
            });
            $('#chkRelatedStudentNo').click(function () {
                $('#txtRelatedStudentName').attr("disabled", "disabled");
                $('#chkPrimary').attr("disabled", "disabled");
                $('#chkSecondary').attr("disabled", "disabled");
                $('#txtStandard').attr("disabled", "disabled");
                $('#txtDivision').attr("disabled", "disabled");
            });
</script>

【问题讨论】:

  • 只需在文档开头放置一个 alert() 即可。阅读并检查此 jquery 是否被触发并恢复的天气
  • 是的。我收到了警报。它被解雇了。虽然我在下面的脚本中放置了一个警报..点击 $("input[name=RelatedStudent]:radio").click(function() { });
  • 然后代码似乎很好,我在小提琴中检查了它,一切似乎都很好,你试过控制台了吗,它给出了什么错误
  • 我在控制台中也没有错误..
  • 我刚刚注意到这一行 nextStepWizard.removeAttr('disabled').trigger('click');。可能是它引起了问题,

标签: javascript jquery asp.net visibility disabled-control


【解决方案1】:

由于 html 元素是在服务器上呈现的,因此元素的 ID 会发生变化。 在javascript中访问ID时,请使用如下:

$('#<%# chkRelatedStudentYes.ClientID%>').removeAttr("disabled");

【讨论】:

  • 嘿,谢谢。我会记下来的!
【解决方案2】:

我发现了问题所在。 该脚本无法通过 ID 或其 NAME 属性找到元素。 我尝试了所有可能的方法来在单击元素时触发脚本,并意识到当我使用它时它正在工作 - $("input[type=radio]:radio") 并且还使用了类名 - $(".form-control") 我修改了脚本并使用了下面的脚本并成功了

$("input[type=radio]:radio").click(function () {
    if ($(this).val() == 'yes') {
        $(".form-control-disable").removeAttr("disabled");
            }
    else if ($(this).val() == 'no') {
        $(".form-control-disable").attr("disabled", "disabled");       
            }
});     

我将以下类用于必须禁用的文本和单选控件“.form-control-disable”并且它有效。 我感谢 Abhi 的耐心和帮助。 :)

【讨论】:

    【解决方案3】:

    你好,你触发事件的方式是错误的

    请试试这个,这里有完整的代码应该可以工作

    注意:测试过

     $(document).ready(function() {
            $("input[name=RelatedStudent]:radio").click(function() { // attack a click event on all radio buttons with name 'radiogroup'
    
    
                    if($(this).val() == 'yes') {//check which radio button is clicked 
                            $('#txtRelatedStudentName').removeAttr("disabled");
                    $('#chkPrimary').removeAttr("disabled");
                    $('#chkSecondary').removeAttr("disabled");
                    $('#txtStandard').removeAttr("disabled");
                    $('#txtDivision').removeAttr("disabled");
                    } else if($(this).val() == 'no') {
                          $('#txtRelatedStudentName').attr("disabled", "disabled");
                    $('#chkPrimary').attr("disabled", "disabled");
                    $('#chkSecondary').attr("disabled", "disabled");
                    $('#txtStandard').attr("disabled", "disabled");
                    $('#txtDivision').attr("disabled", "disabled");
                    } 
            });
    });
    

    你可以在这里试试https://jsfiddle.net/abhiyx/fgen1br9/

    【讨论】:

    • 请填写代码acc。根据您在活动中的喜好
    • 是的,描述中提到的代码存在于 $(document).ready(function()... 仍然没有工作,兄弟
    • 我不是在说 document.ready,我是在说 $("input[name=RelatedStudent]:radio").click(function(),等一下,让我给你弄个小提琴跨度>
    • 我也试过这段代码..我不知道这里出了什么问题..它仍然无法正常工作。
    • 答案已更新,请尝试使用小提琴链接,让我知道它是否有效
    【解决方案4】:

    您可以使用 jQuery prop() 方法代替 removeAttr()。试试这个 -

    $('#chkRelatedStudentYes').click(function () {
       $('#txtRelatedStudentName').prop("disabled", false);
       $('#chkPrimary').prop("disabled", false);
       $('#chkSecondary').prop("disabled", false);
       $('#txtStandard').prop("disabled", false);
       $('#txtDivision').prop("disabled", false);
    });
    

    【讨论】:

      猜你喜欢
      • 2012-07-22
      • 2023-03-23
      • 1970-01-01
      • 2019-09-23
      • 2012-05-21
      • 2023-03-04
      • 2016-12-18
      • 1970-01-01
      相关资源
      最近更新 更多