简体   繁体   English

如果选中单选按钮,则在Codeigniter中显示div

[英]If radio buttons is checked then display div in Codeigniter

I'm trying to create a user form, I am stuck in it bacause I want that if A radio button is checked then display a div.I could not do this as I am little bit weak in javascript. 我正在尝试创建一个用户表单,我被困在其中因为我想要如果选中一个单选按钮然后显示div。我不能这样做因为我在javascript中有点弱。 please find my code below. 请在下面找到我的代码。 My button is 我的按钮是

 <label>
    <input type="radio" name="groups[]" value="6"> Head of Distribution Sales
</label>

And after selecting the button I want to show this div 选择按钮后,我想显示这个div

<div class="form-group">
    <label class="col-md-3 control-label">Regions</label>
    <div class="col-md-9">
            <div class="checkbox-list">
                <label>
                    <input type="checkbox" name="groups[]" value="43"> Dhaka North
                </label>
                <label>
                    <input type="checkbox" name="groups[]" value="44"> Dhaka South
                </label>
                <label>
                    <input type="checkbox" name="groups[]" value="45"> Comilla
                </label>
                <label>
                    <input type="checkbox" name="groups[]" value="46"> Chittagong
                </label>
                <label>
                    <input type="checkbox" name="groups[]" value="47"> Khulna
                </label>
                <label>
                    <input type="checkbox" name="groups[]" value="48"> Mymensingh
                </label>
                <label>
                    <input type="checkbox" name="groups[]" value="49"> Sylhet
                </label>
                <label>
                    <input type="checkbox" name="groups[]" value="50"> Rangpur
                </label>
                <label>
                    <input type="checkbox" name="groups[]" value="51"> Bogra
                </label>
                <label>
                    <input type="checkbox" name="groups[]" value="52"> Barisal
                </label>
            </div>
    </div>
</div>

Please help me guys solving this. 请帮我解决这个问题。

To achieve this you have to make following changes in your HTML and implement jquery code as snippet. 要实现这一点,您必须在HTML中进行以下更改,并将jquery代码实现为代码段。

  1. Give id to the div which you want to show/hide. 将id提供给要显示/隐藏的div。

  2. Initially hide div by giving style="display:none" 最初通过给出style="display:none"隐藏div

  3. place a jquery code to fire on change event on radio button and show/hide div accordingly. 放置一个jquery代码来触发单选按钮上的更改事件并相应地显示/隐藏div。

Please check below snippet. 请查看下面的代码段。

 $("input[type='radio'][name='groups[]']").on("change",function(){ if($(this).is(':checked')){ $("#displayDS").show(); }else{ $("#displayDS").hide(); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label> <input type="radio" name="groups[]" value="6"> Head of Distribution Sales </label> <br/><br/> <div class="form-group" style="display:none" id="displayDS"> <label class="col-md-3 control-label">Regions</label> <div class="col-md-9"> <div class="checkbox-list"> <label> <input type="checkbox" name="groups[]" value="43"> Dhaka North </label> <label> <input type="checkbox" name="groups[]" value="44"> Dhaka South </label> <label> <input type="checkbox" name="groups[]" value="45"> Comilla </label> <label> <input type="checkbox" name="groups[]" value="46"> Chittagong </label> <label> <input type="checkbox" name="groups[]" value="47"> Khulna </label> <label> <input type="checkbox" name="groups[]" value="48"> Mymensingh </label> <label> <input type="checkbox" name="groups[]" value="49"> Sylhet </label> <label> <input type="checkbox" name="groups[]" value="50"> Rangpur </label> <label> <input type="checkbox" name="groups[]" value="51"> Bogra </label> <label> <input type="checkbox" name="groups[]" value="52"> Barisal </label> </div> </div> </div> 

I add some IDs to simplifier 我为简化器添加了一些ID

// HTML
<label>
  <input type="radio" id="myRadio" name="groups[]" value="6"> Head of Distribution Sales
</label>

<div id="yourDiv" class="form-group" style="display:none;"> [... checkboxes ...] </div>


// JS
document.getElementById('myRadio').addEventListener('change', function(){
  document.getElementById('yourDiv').style.display = this.checked ? 'block' : 'none';
});

 document.getElementById('myRadio').addEventListener('change', function(){ document.getElementById('yourDiv').style.display = this.checked ? 'block' : 'none'; }); 
 <label> <input type="radio" id="myRadio" name="groups[]" value="6"> Head of Distribution Sales </label> <div id="yourDiv" class="form-group" style="display:none;"> <label class="col-md-3 control-label">Regions</label> <div class="col-md-9"> <div class="checkbox-list"> <label> <input type="checkbox" name="groups[]" value="43"> Dhaka North </label> <label> <input type="checkbox" name="groups[]" value="44"> Dhaka South </label> <label> <input type="checkbox" name="groups[]" value="45"> Comilla </label> <label> <input type="checkbox" name="groups[]" value="46"> Chittagong </label> <label> <input type="checkbox" name="groups[]" value="47"> Khulna </label> <label> <input type="checkbox" name="groups[]" value="48"> Mymensingh </label> <label> <input type="checkbox" name="groups[]" value="49"> Sylhet </label> <label> <input type="checkbox" name="groups[]" value="50"> Rangpur </label> <label> <input type="checkbox" name="groups[]" value="51"> Bogra </label> <label> <input type="checkbox" name="groups[]" value="52"> Barisal </label> <label> <input type="radio" name="groups[]" value="6"> Head of Distribution Sales </label> </div> </div> </div> 

Use jQuery : 使用jQuery:

$(document).ready(function() {
$('input[type="radio"]').click(function() {
   if($(this).attr('id') == 'your input id') {
        $('#div id').show();           
   }

   else {
        $('#div id').hide();   
   }
});
});

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

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