首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据使用javascript的复选框显示Textarea条件条件。

根据使用javascript的复选框显示Textarea条件条件。
EN

Stack Overflow用户
提问于 2021-09-28 06:11:35
回答 1查看 26关注 0票数 1

出于某种原因我正在做一份清单。如果单击“ other ”检查表,将出现一个textarea,当检查其他原因时,textarea将消失(添加隐藏类)。目前,当我检查 other 时,会出现textarea,但当我检查其他原因时,textarea不会消失。

我的密码有什么问题吗?

代码语言:javascript
复制
$(document).ready(function(){
  $("#cancel-reasons > .custom-checkbox").each(function() {
     const el = $(this);
     el.on('click', function(val) {
        if ($(el).children("input").attr("value") == 1) {
           $(el).children("input").attr('value', 0);
           $(el).children("input").prop('checked', false);
        } else {
           $(el).children("input").attr('value', 1);
           $(el).children("input").prop('checked', true);
        }
        if($(el).children("input").hasClass("other-reason-checkbox")) {
           if (($(el).children("input").attr("value") == 1) && ($(el).children("span").text() === "Others")) {
               $("#other-reason-textarea").removeClass("hidden")
           } else {
               $("#other-reason-textarea").addClass("hidden")
         }
      }
    });
  });
});
代码语言:javascript
复制
.hidden{
  display:none;
}
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="cancel-reasons" id="cancel-reasons">
  <div class="custom-control custom-checkbox">
    <input class='custom-control-input cancel-reason-checkbox' name="reason-check" value='0' data-reasonid="type-1" type="radio">
    <label class="custom-control-label" id="type-1" for="type-1"></label>
    <span>Reason1</span>
  </div>
  <div class="custom-control custom-checkbox">
    <input class='custom-control-input cancel-reason-checkbox' name="reason-check" value='0' data-reasonid="type-2" type="radio">
    <label class="custom-control-label" id="type-2" for="type-2"></label>
    <span>Reason 2</span>
  </div>
  <div class="custom-control custom-checkbox">
    <input class='custom-control-input cancel-reason-checkbox other-reason-checkbox' name="reason-check" value='0' data-reasonid="type-other" type="radio">
    <label class="custom-control-label" id="type-other" for="type-other"></label>
    <span>Others</span>
  </div>

  <textarea name="order-cancelTextArea" maxlength="100" id="other-reason-textarea" class="hidden" cols="30" rows="5"></textarea>
</div>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-28 06:21:50

这是因为当textarea是假的时候,没有隐藏$(el).children("input").hasClass("other-reason-checkbox")

代码语言:javascript
复制
$(document).ready(function(){
  $("#cancel-reasons > .custom-checkbox").each(function() {
     const el = $(this);
     el.on('click', function(val) {
        if ($(el).children("input").attr("value") == 1) {
           $(el).children("input").attr('value', 0);
           $(el).children("input").prop('checked', false);
        } else {
           $(el).children("input").attr('value', 1);
           $(el).children("input").prop('checked', true);
        }
        if($(el).children("input").hasClass("other-reason-checkbox")) {
           if (($(el).children("input").attr("value") == 1) && ($(el).children("span").text() === "Others")) {
               $("#other-reason-textarea").removeClass("hidden")
           } else {
               $("#other-reason-textarea").addClass("hidden")
         }
      }
       else {
                    $("#other-reason-textarea").addClass("hidden")
                }
    });
  });
});
代码语言:javascript
复制
.hidden{
  display:none;
}
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="cancel-reasons" id="cancel-reasons">
  <div class="custom-control custom-checkbox">
    <input class='custom-control-input cancel-reason-checkbox' name="reason-check" value='0' data-reasonid="type-1" type="radio">
    <label class="custom-control-label" id="type-1" for="type-1"></label>
    <span>Reason1</span>
  </div>
  <div class="custom-control custom-checkbox">
    <input class='custom-control-input cancel-reason-checkbox' name="reason-check" value='0' data-reasonid="type-2" type="radio">
    <label class="custom-control-label" id="type-2" for="type-2"></label>
    <span>Reason 2</span>
  </div>
  <div class="custom-control custom-checkbox">
    <input class='custom-control-input cancel-reason-checkbox other-reason-checkbox' name="reason-check" value='0' data-reasonid="type-other" type="radio">
    <label class="custom-control-label" id="type-other" for="type-other"></label>
    <span>Others</span>
  </div>

  <textarea name="order-cancelTextArea" maxlength="100" id="other-reason-textarea" class="hidden" cols="30" rows="5"></textarea>
</div>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69356616

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档