0

我有一个简单的 html 表单,表格中有一系列问题。如果用户对是/否单选按钮问题回答是,那么我想显示一个隐藏行,允许他们在文本区域字段中输入详细信息。如果他们单击否,则应清除并再次隐藏 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演示我目前的表单。我的可选行一开始是隐藏的,但是当您单击是单选按钮时不可见。

4

4 回答 4

2

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

于 2013-07-25T14:40:35.680 回答
0

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

我的建议:

_y分别将和_n(或任何您喜欢的)附加到 ID。(或使用任何其他唯一ID)

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

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

$("#question1_n").click(function () {
    $(".question1yes").hide();
    $(".question1yes textarea").val("");
});
于 2013-07-25T15:05:29.163 回答
0

在它正常工作之后尝试这个

$("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 上实现的类。

希望对你有帮助

于 2013-07-25T15:02:44.917 回答
0

尝试这个

$().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("");
                }
                });

    });

演示

于 2013-07-25T14:37:34.280 回答