繁体   English   中英

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

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

我有一个简单的html表单,表格中有一系列问题。 如果用户对是/否单选按钮问题回答“是”,则我想显示一个隐藏行,允许他们将详细信息输入到textarea字段中。 如果他们单击否,则应清除并再次隐藏textarea输入。

这是我的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 演示我目前的形式。 我的可选行以隐藏方式启动,但单击“是”单选按钮时不会显示。

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

尝试这个

$().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()。 question1yes是在tr而不是id上实现的类。

希望它会对你有所帮助

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

我的建议:

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

将点击事件绑定到新ID。
码:

$("#question1_y").click(function () {
    $(".question1yes").show();
});

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

暂无
暂无

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

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