单击选项时的事件委托不适用于克隆的元素

Event delegation on click of an option, doesn't work on cloned element

我之前的问题(已关闭)-> Jquery - Cloning a div which includes input unordered lists. How to get the dropdown to work in the cloned row?

我参考了这个答案:-> Event binding on dynamically created elements?

我一直在尝试使用哪个静态祖先,克隆版本不会记录任何点击。

例如,在我的第一个版本中,我使用 .on as so.. 从下拉列表中单击一个选项到 运行 一个函数,该函数将分配一个 class 选定的,这将取消隐藏下一个下拉菜单。

$(".option").on("click", unhideoption2);

但是对于我添加的每个 parent/static 选择器,它仍然没有在克隆版本上注册任何点击

例如

$(".optionlist").on("click", ".option", unhideoption2);

$(".cselect").on("click", ".option", unhideoption2);

$(".row1").on("click", ".option", unhideoption2);

我应该将事件委托添加到正在克隆的行而不是单个输入吗?虽然当我也尝试这样做时,它仍然没有记录对克隆行的点击。

$(".rows").on("click",'.clonerow', clonerow);

事件委托哪里出错了?

https://jsfiddle.net/pfhnr9uk/4/

HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="rows">
<div class="row1">
  <div class="cselect options1">
    <input type="text" disabled placeholder="Select n1">
    <ul class="optionlist">
      <li class="option option1">Business</li>
      <li class="option option2">Hair</li>
    </ul>
  </div>
<div class="cselect options2 hide">
    <input type="text" disabled placeholder="new test">
    <ul class="optionlist">
      <li class="option option1">test</li>
      <li class="option option2">option 2</li>
    </ul>
  </div>
</div>
<div class="row2">

  <div class="cselect options3">
    <input type="text" disabled placeholder="Select n2">
    <ul class="optionlist">
      <li class="option">Something</li>
      <li class="option">Else</li>
    </ul>
  </div>
</div>
<div class="clonerow">
click me
</div>
</div>

CSS

* {
  margin: 0;
  padding: 0;
}

/* ugly reset */

.cselect {
  position: relative;
}

.cselect input {
  background: #fff;
}

.cselect ul {
  display: none;
  position: absolute;
  z-index: 999;
  left: 0;
  top: 1.2rem;
  margin: 0;
  width: 100%;
  background: #fff;
  border: 1px solid #d6d6d6;
}

.cselect li {
  padding: 10px 5%;
  list-style: none;
}

.cselect li:hover {
  background: rgba(41, 128, 185, 0.2);
}

.hide{
  display:none;
}

JS

$(function() { // DOM ready


  $(".cselect").each(function() {

    var $input = $(this).find("input");
    var $dropDown = $(this).find("ul");

    $(this).on("click", function() {
      $dropDown.stop().slideToggle();
    });

    $dropDown.on("click", "li", function() {
      $input.val($(this).text());
    });

  });

});

var newnewid = 0;
var $cloneplayerclause = jQuery(".row1").clone(true);

function clonerow(){
  
  newnewid++;
  var $sectionClone = $cloneplayerclause.attr("id", newnewid).clone(true);
  $('.rows').append($sectionClone);
  
  
}

function unhideoption2(){
 $(".option").removeClass('selected');
 $(this).addClass('selected');
 if($('.option1').hasClass('selected')){
 $('.options2').removeClass('hide');
 }
 }

$(".option").on("click", unhideoption2);
$(".clonerow").on("click", clonerow);

如何操作的基本示例。

关键在这里:

function clonerow(){

        let cloneplayerclause = $(".row1").clone(false);

      let sectionClone = $(cloneplayerclause).attr("class", "rowx").find(".cselect").each(function() {

        var $input = $(this).find("input");
        var $dropDown = $(this).find("ul");

        $(this).on("click", function() {
          $dropDown.stop().slideToggle();
        });

        $dropDown.on("click", "li", function() {
          $input.val($(this).text());
        });

      });
      
      $('.rows').append(sectionClone);
      
      
    }

完整示例,试用示例:

$(function(){


  $(".cselect").each(function(){

    var $input = $(this).find("input");
    var $dropDown = $(this).find("ul");

    $(this).on("click", function() {
      $dropDown.stop().slideToggle();
    });

    $dropDown.on("click", "li", function() {
      $input.val($(this).text());
    });

  });
  
  let cloneplayerclause = $(".row1").clone(false);
  
  window.cloneplayerclause = cloneplayerclause;

});


function clonerow(){

    let sectionClone = $("<div class='rowx' />")
    
    $(cloneplayerclause).clone(true, true).find(".cselect").each(function(){

    var $input = $(this).find("input");
    var $dropDown = $(this).find("ul");

    $(this).on("click", function() {
      $dropDown.stop().slideToggle();
    });

    $dropDown.on("click", "li", function() {
      $input.val($(this).text());
    });
    
    $(sectionClone).append(this);

  });
  
  $('.rows').append(sectionClone);
  
  $(".option").on("click", unhideoption2);
  
}

function unhideoption2(){
 $(".option").removeClass('selected');
 $(this).addClass('selected');
 if($(this).parent().parent().parent().find('.options1 ul li').hasClass("selected")){
 $(this).parent().parent().parent().find('.options2').removeClass('hide');
 }
 }

$(".option").on("click", unhideoption2);
$(".clonerow").on("click", clonerow);
* {
  margin: 0;
  padding: 0;
}

/* ugly reset */

.cselect {
  position: relative;
}

.cselect input {
  background: #fff;
}

.cselect ul {
  display: none;
  position: absolute;
  z-index: 999;
  left: 0;
  top: 1.2rem;
  margin: 0;
  width: 100%;
  background: #fff;
  border: 1px solid #d6d6d6;
}

.cselect li {
  padding: 10px 5%;
  list-style: none;
}

.cselect li:hover {
  background: rgba(41, 128, 185, 0.2);
}

.hide{
  display:none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>

    <head>
    
        <script type="text/javascript" src="jquery-3.5.1.min.js"></script>
    
    </head>
    
    <body>
    
        <div class="rows">
        <div class="row1">
          <div class="cselect options1">
            <input type="text" disabled placeholder="Select n1">
            <ul>
              <li class="option option1">Business</li>
              <li class="option option2">Hair</li>
            </ul>
          </div>
        <div class="cselect options2 hide">
            <input type="text" disabled placeholder="new test">
            <ul>
              <li class="option option1">test</li>
              <li class="option option2">option 2</li>
            </ul>
          </div>
        </div>
        <div class="row2">

          <div class="cselect options3">
            <input type="text" disabled placeholder="Select n2">
            <ul>
              <li class="option">Something</li>
              <li class="option">Else</li>
            </ul>
          </div>
        </div>
        <div class="clonerow">
        click me
        </div>
        </div>

        <script type="text/javascript" src="index.js"></script>

    </body>

</html>

我在unhideoption2函数中让选项部分更加动态

if($(this).parent().parent().parent().find('.options1 ul li').hasClass("selected")){
 $(this).parent().parent().parent().find('.options2').removeClass('hide');
 }

事件处理程序未被克隆,但您可以像这样在 .rows 框上创建事件处理程序:

$(function() { // DOM ready
  const rows = $(".rows").on("click", function(e) {
    if (e.target.tagName == "INPUT") {
      const input = rows.find(e.target);
      input.parent().find("ul").stop().slideToggle();
    } else if (e.target.classList.contains("option")) {
      const li = e.target;
      const ul = li.parentNode;
      const divSelect = ul.parentNode;
      const row = divSelect.parentNode;
      const input = divSelect.querySelector("input");
      if (!divSelect.dataset.hidden)
        row.dataset.hidden = li.dataset.hidden;

      input.value = li.textContent;
      $(input).click();
      for(let i = 0; i < ul.children.length; i++)
      {
        ul.children[i].classList.toggle("selected", ul.children[i] === li);
      }
      const hiddenRows = row.querySelectorAll(".cselect[data-hidden]");
      for(let i = 0; i < hiddenRows.length; i++)
      {
        hiddenRows[i].classList.toggle("hide", !row.dataset.hidden || row.dataset.hidden != hiddenRows[i].dataset.hidden);
      }
    }
  });
});
var newnewid = 0;
var $cloneplayerclause = jQuery(".row1").clone(true);

function clonerow() {

  newnewid++;
  var $sectionClone = $cloneplayerclause.attr("id", newnewid).clone(true);
  $('.rows').append($sectionClone);


}

$(".clonerow").on("click", clonerow);
* {
  margin: 0;
  padding: 0;
}


/* ugly reset */

.cselect {
  position: relative;
}

.cselect input {
  background: #fff;
}

.cselect ul {
  display: none;
  position: absolute;
  z-index: 999;
  left: 0;
  top: 1.2rem;
  margin: 0;
  width: 100%;
  background: #fff;
  border: 1px solid #d6d6d6;
}

.cselect li {
  padding: 10px 5%;
  list-style: none;
}

.cselect li:hover {
  background: rgba(41, 128, 185, 0.2);
}

.hide {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="rows">
  <div class="row1">
    <div class="cselect options1">
      <input type="text" disabled placeholder="Select n1">
      <ul>
        <li class="option option1" data-hidden="1">Business</li>
        <li class="option option2">Hair</li>
        <li class="option option3" data-hidden="3">Face</li>
      </ul>
    </div>
    <div class="cselect options2 hide" data-hidden="1">
      <input type="text" disabled placeholder="new test">
      <ul>
        <li class="option option1">test</li>
        <li class="option option2">option 2</li>
      </ul>
    </div>
    <div class="cselect options2 hide" data-hidden="3">
      <input type="text" disabled placeholder="face type">
      <ul>
        <li class="option option1">type 1</li>
        <li class="option option2">type 2</li>
      </ul>
    </div>
  </div>
  <div class="row2">

    <div class="cselect options3">
      <input type="text" disabled placeholder="Select n2">
      <ul>
        <li class="option">Something</li>
        <li class="option">Else</li>
      </ul>
    </div>
  </div>
  <div class="clonerow">
    click me
  </div>
</div>