根据“是/否”单选按钮有条件地显示/隐藏表行

Conditionally Show/Hide Table Row based on yes/no radio button

本文关键字:显示 隐藏 有条件 单选按钮 根据      更新时间:2023-09-26

我有一个简单的html表单,表中有一系列问题。如果用户对"是/否"单选按钮问题回答"是",那么我想显示一个隐藏行,允许他们在文本区域字段中输入详细信息。如果单击"否",则文本区域输入应被清除并再次隐藏。

这是我的html表单,有一个是/否问题和一个隐藏行,如果他们点击是,可以获得更多详细信息:

<form class="form-horizontal" action="#" method="post" id="questionForm">
      <input type="hidden" name="recid" value="1">
      <table class="table table-condensed table-hover table-bordered">
          <tr>
          <td><strong>Question 1</strong></td>
          <td>please answer yes or no to this question</td>
          <td>
              <div class="controls">
                  <label class="radio inline">
          <input type="radio" name="question1" id="question1" value="Yes" required>Yes        </label>
                  <label class="radio inline">
          <input type="radio" name="question1" id="question1" value="No" required>No          </label>
                  <label for="question1" class="error"></label>
        </div>
          </td>
          </tr>

          <tr class="question1yes">
          <td></td>
          <td>Please describe this and when it started</td>
          <td>
              <div class="controls">
          <textarea name="question1Details" rows="3"></textarea>
          <label for="question1Details" class="error"></label>
        </div>
          </td>
          </tr>
          </table>
</div>              
                    <div class="control-group">
            <div class="controls">
              <button type="submit" class="btn btn-primary">Continue</button>
              <button type="reset" class="btn">Reset</button>
            </div>
        </div>
      </form>

这是我目前不起作用的脚本:

$().ready(function() {
        // validate the form when it is submitted
        $("#questionForm").validate();
        if($("#question1:checked").length != 0){
                    // yes is checked... show the dependent fields  
                        $(".question1yes").show(); 
                    }else{
                        // hide it and blank the fields, just in case they have something in them
                        $(".question1yes").hide(); 
                        $("#question1Details").val("");
                    }

        $("#question1").click(function(){ 
                    // show the dependent fields
                    if(this.value == "Yes"){
                        $("#question1yes").show();
                    }else{
                        // hide the dependent fields and blank them
                        $(".question1yes").hide();
                        $("#question1Details").val("");
                    }
                    });
        });

我在这里设置了一个jsFiddle,它展示了我目前的状态。我的可选行从隐藏开始,但单击"是"单选按钮时不可见。

不能有两个具有相同ID的元素,要选择它们,可以使用$('.controls input[type="radio"]')或类选择器,例如$('.radio')

您的无线电输入具有相同的ID,这不是有效的HTML。此外,它破坏了您的代码,因为您无法独立操作它们。

我的建议:

_y_n(或您喜欢的任何内容)分别附加到ID。(或使用任何其他唯一ID)

将单击事件绑定到新ID
代码:

$("#question1_y").click(function () {
    $(".question1yes").show();
});
$("#question1_n").click(function () {
    $(".question1yes").hide();
    $(".question1yes textarea").val("");
});

试试这个

$().ready(function() {
    // validate the form when it is submitted
    $("#questionForm").validate();
    if($("#question1:checked").length > 0){
                // yes is checked... show the dependent fields  
                    $(".question1yes").show(); 
                }else{
                    // hide it and blank the fields, just in case they have something in them
                    $(".question1yes").hide(); 
                    $("#question1Details").val("");
                }

    $("#question1").click(function(){ 
                // show the dependent fields
                if($(this).val() == "Yes"){
                    $("#question1yes").show();
                }else{
                    // hide the dependent fields and blank them
                    $(".question1yes").hide();
                    $("#question1Details").val("");
                }
                });
    });

演示

尝试以下操作,因为它应该是

$("input:radio[name=question1]").click(function(){ 
  // show the dependent fields
  if(this.value == "Yes"){
    $(".question1yes").show();
  }else{
    // hide the dependent fields and blank them
    $(".question1yes").hide();
    $("#question1Details").val("");
  }
});

问题是您正在调用$("#question1yes").show();而不是$(".question1yes").show().question2yes是在tr而非id上实现的类。

希望它能帮助你