【问题标题】:how to show and hide field based on radio button如何根据单选按钮显示和隐藏字段
【发布时间】:2016-10-20 14:58:51
【问题描述】:

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

这是我的代码

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

任何人都可以帮忙,因为我没有太多的javascript经验

【问题讨论】:

  • javascript不只是java对
  • PHP 代码不需要在这里,并且会阻止有用的 cmets。你能用生成的 HTML 编辑你的消息吗?

标签: php html forms radio-button field


【解决方案1】:

这是使用 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> 

在这里,我使用onchange() 事件来更改值。

你的部门:

<div id="yourfield" style="display:none;"> 
   Hide Me: Place your all four fields here
   student_date_of_graduate , 
   average , 
   schoolname and 
   specialty
</div>

您需要一个标识符 id="yourfield" 来执行更改,例如显示和隐藏特定的 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>

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

【讨论】:

  • 为什么我使用基本示例,因为我们不知道您要显示或隐藏哪个块。
  • 我需要通过默认隐藏所有接下来的 4 个字段,并且只有在用户选择是时才会出现,如果用户选择否则再次隐藏......字段名称是 student_date_of_graduate 、 average 、 schoolname 和 special
  • @MustafaShafout:您需要在 div 中添加所有这四个选项,就像我在 HIDE ME 代码中使用的那样,而不是为此 div 添加 id 属性
  • @MustafaShafout:我已经更新了我的答案,希望对你有所帮助
  • 我正在尝试,但我有一些错误......现在四个字段被隐藏了,但当我选择是时它仍然隐藏
【解决方案2】:

使用 jQuery :

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

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

【讨论】:

    猜你喜欢
    • 2013-07-11
    • 2021-10-01
    • 1970-01-01
    • 2021-12-02
    • 2020-12-28
    • 1970-01-01
    • 2013-08-05
    • 2018-02-27
    • 2021-09-02
    相关资源
    最近更新 更多