Php 如何基于单选按钮显示和隐藏字段

Php 如何基于单选按钮显示和隐藏字段,php,html,forms,radio-button,field,Php,Html,Forms,Radio Button,Field,我有一个单选按钮问题,答案是或不是。我需要显示和隐藏下一个字段取决于单选按钮的答案,如果是,显示字段如果否,保持隐藏 这是我的密码 <?php if(form_error('HSC')) echo "<div class='form-group has-error' >"; else ech

我有一个单选按钮问题,答案是或不是。我需要显示和隐藏下一个字段取决于单选按钮的答案,如果是,显示字段如果否,保持隐藏

这是我的密码

<?php 
                    if(form_error('HSC')) 
                        echo "<div class='form-group has-error' >";
                    else     
                        echo "<div class='form-group' >";
                ?>
                    <label for="HSC" class="col-sm-2 control-label">
                        <?=$this->lang->line("student_have_hs")?>
                    </label>
                    <div class="col-sm-3">
                        <input type="radio" name="HSC" value="Yes">Yes<br> <input type="radio" name="HSC" value="No"> No<br> 
                    </div>
                    <span class="col-sm-4 control-label">
                        <?php echo form_error('HSC'); ?>
                    </span>
                </div>

                <?php 
                    if(form_error('student_date_of_graduate')) 
                        echo "<div class='form-group has-error' >";
                    else     
                        echo "<div class='form-group' >";
                ?>
                    <label for="student_date_of_graduate" class="col-sm-2 control-label">
                        <?=$this->lang->line("student_date_of_graduate")?>
                    </label>
                    <div class="col-sm-6">
                        <input type="text" class="form-control" id="student_date_of_graduate" name="student_date_of_graduate" value="<?=set_value('student_date_of_graduate')?>" >
                    </div>
                    <span class="col-sm-4 control-label">
                        <?php echo form_error('student_date_of_graduate'); ?>
                    </span>
                </div>

                <?php
                if(form_error('schoolname'))
                    echo "<div class='form-group has-error' >";
                else
                    echo "<div class='form-group' >";
                ?>
                    <label for="name_id" class="col-sm-2 control-label">
                       <?=$this->lang->line("student_schoolname")?>
                    </label>
                    <div class="col-sm-6">
                    <input type="text" class="form-control" id="schoolname" name="schoolname" value="<?=set_value('schoolname')?>" >
                    </div>
                    <span class="col-sm-4 control-label">
                        <?php echo form_error('schoolname'); ?>
                    </span>
                </div>

                <?php 
                    if(form_error('specialty')) 
                        echo "<div class='form-group has-error' >";
                    else     
                        echo "<div class='form-group' >";
                ?>
                    <label for="specialty" class="col-sm-2 control-label">
                        <?=$this->lang->line("student_specialty")?>
                    </label>
                    <div class="col-sm-6">
                        <input type="text" class="form-control" id="specialty" name="specialty" value="<?=set_value('specialty')?>" >
                    </div>
                    <span class="col-sm-4 control-label">
                        <?php echo form_error('specialty'); ?>
                    </span>
                </div>

                <?php 
                    if(form_error('average')) 
                        echo "<div class='form-group has-error' >";
                    else     
                        echo "<div class='form-group' >";
                ?>
                    <label for="average" class="col-sm-2 control-label">
                        <?=$this->lang->line("student_average")?>
                    </label>
                    <div class="col-sm-6">
                        <input type="text" class="form-control" id="average" name="average" value="<?=set_value('average')?>" >
                    </div>
                    <span class="col-sm-4 control-label">
                        <?php echo form_error('average'); ?>
                    </span>
                </div>
使用jQuery:

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

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

下面是使用javascript的基本示例:

单选按钮:

<input type="radio" name="HSC" value="Yes" onChange="getValue(this)">Yes<br> 
<input type="radio" name="HSC" value="No" onChange="getValue(this)"> No<br> 
<div id="yourfield" style="display:none;"> 
   Hide Me: Place your all four fields here
   student_date_of_graduate , 
   average , 
   schoolname and 
   specialty
</div>
<script type="text/javascript">
function getValue(x) {
  if(x.value == 'No'){
    document.getElementById("yourfield").style.display = 'none'; // you need a identifier for changes
  }
  else{
    document.getElementById("yourfield").style.display = 'block';  // you need a identifier for changes
  }
}
</script>
您需要一个标识符
id=“yourfield”
来执行更改,如显示和隐藏特定的div

脚本:

<input type="radio" name="HSC" value="Yes" onChange="getValue(this)">Yes<br> 
<input type="radio" name="HSC" value="No" onChange="getValue(this)"> No<br> 
<div id="yourfield" style="display:none;"> 
   Hide Me: Place your all four fields here
   student_date_of_graduate , 
   average , 
   schoolname and 
   specialty
</div>
<script type="text/javascript">
function getValue(x) {
  if(x.value == 'No'){
    document.getElementById("yourfield").style.display = 'none'; // you need a identifier for changes
  }
  else{
    document.getElementById("yourfield").style.display = 'block';  // you need a identifier for changes
  }
}
</script>

函数getValue(x){
如果(x.value==“否”){
document.getElementById(“yourfield”).style.display='none';//更改需要标识符
}
否则{
document.getElementById(“yourfield”).style.display='block';//您需要一个用于更改的标识符
}
}

简单的javascript函数,仅用于根据单选按钮值显示或隐藏您的div。

javascript不仅java rightPHP代码不需要在这里,而且可以防止有用的注释。你能用生成的HTML编辑你的邮件吗?为什么我使用基本示例,因为我们不知道你要显示或隐藏哪个块。我需要按defult隐藏接下来的所有4个字段,并且仅当用户选择“是”时才显示,如果用户选择“否”,则再次隐藏。。。。。字段名称为学生、毕业日期、平均值、学名和specialty@MustafaShafout:您需要在div中添加所有这四个选项,因为我在代码中使用隐藏我,而不是为此添加
id
属性div@MustafaShafout:我已经更新了我的答案,我希望这将有助于我尝试,但我有一些错误。。。现在这四个字段被隐藏,但当我选择“是”时,它仍然是隐藏的