简体   繁体   English

如果选中,则显示其他复选框

[英]If checked, show different checkbox

I am in need of building a form, all checkboxes, which show one after another. 我需要建立一个表单,所有复选框,一个接一个显示。 So if one checkbox is checked, another shows below. 因此,如果选中了一个复选框,则下面将显示另一个复选框。 Only when the one above is checked. 仅当选中以上一项时。 The form will contain hundreds of checkboxes as many options. 表单将包含数百个与许多选项一样的复选框。 so Ifelse statements wouldn't be suitable. 因此,Ifelse语句不合适。 The data for each checkbox will be parsed from xml file. 每个复选框的数据将从xml文件中解析。 The only code that seems to stick for this is 似乎唯一可以坚持的代码是

$('#chkCheckBox').on('click', function (){

    var nextID, body, nextEl;

    nextID = $(this).attr(target);
    nextEl = $('#' + nextID);

    if(nextEl.style.visibility == 'hidden') {
        nextEl.style.visibility = 'display'
    }

})

Just after a little guidance please. 请稍作指导。 Hope I made sense, feel like I'm going round in circles. 希望我有道理,觉得我正在转圈。

nextEl is a jquery object, not a DOM object, so has no style property. nextEl是一个jquery对象,而不是DOM对象,因此没有style属性。 Use jquery .show() and .is() 使用jquery .show().show() .is()

if(!nextEl.is(':visible')) {
     nextEl.show()
}

You also appear to be using the same id for every checkbox ( chkCheckBox ), dont do this, instead use a class and attach your jquery event to that. 您似乎还为每个复选框( chkCheckBox )使用了相同的ID,请不要这样做,而是使用一个类并将jquery事件附加到该类。

 $('.cb').on('click',function(){ nextID = $(this).attr('target'); nextEl = $('#' + nextID); if(!nextEl.is(':visible')) { nextEl.show(); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div><input id="cb1" type="checkbox" class="cb" target="cb2"></div> <div><input id="cb2" type="checkbox" class="cb" target="cb3" style="display:none"></div> <div><input id="cb3" type="checkbox" class="cb" target="cb4" style="display:none"></div> 

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

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