首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >广播单击事件不使用相同类名的复选框

广播单击事件不使用相同类名的复选框
EN

Stack Overflow用户
提问于 2022-03-07 18:52:14
回答 2查看 47关注 0票数 2

我有一个类似类名anotherCheese的复选框,当有人单击它将被附加到steps行-头等舱时,所附加的还有一个带有类名anotherCheese的单选按钮,但是我的单击事件没有被调用到带有类名anotherCheese的新单选按钮上,我做错了什么?

这是HTML

代码语言:javascript
复制
<div class="steps-row-first">
<div class="radio-wrapper">
              <div class="radio-group">
                <input type="radio" class="anotherCheese" name="anotherCheese" value="yes" />
                <label>Yes</label>
              </div>
              <div class="radio-group">
                <input type="radio" name="anotherCheese" value="no" />
                <label>No</label>
              </div>
            </div>
</div>

jQuery:

代码语言:javascript
复制
$('.anotherCheese').on("click", function(){
    if($(this).val() == 'yes')
    {
      $(".steps-row-first").append('<div class="radio-wrapper"> <div class="radio-group"> <input type="radio" class="anotherCheese" name="anotherCheese" value="yes"/> <label>Yes</label> </div><div class="radio-group"> <input type="radio" name="anotherCheese" value="no"/> <label>No</label> </div></div>');
    }
  });
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-03-07 20:14:36

将事件绑定到像.step-row-first这样的祖先元素。下面的示例有一些更改,但本质上,通过绑定到一个祖先元素,您可以利用事件冒泡并控制子元素的所有行为--这称为事件委托

在以下示例中:

  • .step-row-first<form>
  • “单击”事件现在是“变化”事件
  • 注意第二个参数是:".anotherCheese",它将它指定为this
  • <div>改为<fieldset> for 语义学

顺便说一句,由于您没有使用任何#id(很好的选择),您可以使用.clone(),您只需在追加克隆之前不检查它。

代码语言:javascript
复制
$(".steps-row-first").on("change", ".anotherCheese", function() {
  if ($(this).val() == 'yes') {
    $(".steps-row-first").append(`
    <fieldset class="radio-wrapper">
      <fieldset class="radio-group">
        <input type="radio" class="anotherCheese" name="anotherCheese" value="yes" />
        <label>Yes</label><br>
        <input type="radio" name="anotherCheese" value="no" />
        <label>No</label>
      </fieldset>
    </fieldset>`);
  }
});
代码语言:javascript
复制
<body>
  <form class="steps-row-first">
    <fieldset class="radio-wrapper">
      <fieldset class="radio-group">
        <input type="radio" class="anotherCheese" name="anotherCheese" value="yes" />
        <label>Yes</label><br>
        <input type="radio" name="anotherCheese" value="no" />
        <label>No</label>
      </fieldset>
    </fieldset>
  </form>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script>
    /* Make sure that all of the <script> tags are right before the closing </body> tag */
  </script>
</body>

票数 1
EN

Stack Overflow用户

发布于 2022-03-07 19:01:09

您不是从'开始,而是从jquery固定代码中的开始

代码语言:javascript
复制
$('.anotherCheese').on("click", function(){
    if($(this).val() == 'yes')
    {
      $(".steps-row-first").append('<div class="radio-wrapper"> 
<div class="radio-group"> <input type="radio" 
class="anotherCheese" name="anotherCheese" value="yes"/> 
<label>Yes</label> </div><div class="radio-group"> <input 
type="radio" name="anotherCheese" value="no"/> 
<label>No</label> </div></div>');
    }
  });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71385914

复制
相关文章

相似问题

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