简体   繁体   English

由代码生成的输入元素上的访问keyup事件

[英]Access keyup event on input element generated by code

On my project, I generate div elements that contain input fields with the request response data from my database given the search parameters. 在我的项目中,我生成了div元素,这些元素包含输入字段,并带有给定搜索参数的数据库中的请求响应数据。 I need to be able to use a keyup event on it to do some extra work, however the class selector does not work when the input was generated by using append, even though it has the class on inspection. 我需要能够在其上使用keyup事件来做一些额外的工作,但是,即使使用类检查了输入,使用添加生成输入时,类选择器也不起作用。

I created a CodePen that demonstrates it: http://codepen.io/leofontes/pen/PNgabB?editors=1111 我创建了一个演示它的CodePen: http ://codepen.io/leofontes/pen/PNgabB?editors=1111

HTML 的HTML

<div class="container" id="container">
  <button id="button">Click</button><br>
</div>

CSS 的CSS

.container {
  background-color: #F00;
  height: 200px;
  width: 200px;
}

.dynamic-input {
  margin: 5px;
}

Javascript / jQuery Javascript / jQuery

$(document).ready(function () {
  $('#button').click(function() {
    for(var i = 0; i < 5; i++) {
      $('#container').append('<input type="text" class="dynamic-input">')
    }
  });


  $('.dynamic-input').keyup(function() {
    console.log($(this).val());
  });
});

The jQuery code works great, I tested it with inputs generated within the $(document).ready function and it did what I intended, I don't understand why it doesn't when it is generated later on. jQuery代码的效果很好,我使用$(document).ready函数中生成的输入对它进行了测试,并且达到了我的预期目的,我不明白为什么以后生成它时不这样做。

I also tried having a hidden input with the class already loaded with the HTML but that doesn't work either. 我还尝试过使用已加载HTML的类进行隐藏输入,但这也不起作用。

All ideas or suggestions are appreciated, thank you 所有的想法或建议表示赞赏,谢谢

You need event delegation for dynamically generated element. 您需要事件委托来动态生成元素。

$('#container').on('keyup', '.dynamic-input', function () {
    console.log($(this).val());
});

try this : 尝试这个 :

$(document).ready(function () {
  $('#button').click(function() {
    for(var i = 0; i < 5; i++) {
      $('#container').append('<input type="text" class="dynamic-input">')
    }
  });


  $(document).on('keyup', '.dynamic-input', function() {
    console.log($(this).val());
  });
});

The issue is that you are creating the button separately from creating the binding. 问题是您要创建按钮而不是创建绑定。 What you should be doing is creating the input AND binding the click event as part of the callback. 您应该做的是创建输入并绑定click事件作为回调的一部分。 Right now, you're binding the element correctly, syntax-wise, but it doesn't exist yet. 现在,您正在正确地按语法绑定元素,但是尚不存在。 So, what you would like is. 所以,您想要的是。

$(document).ready(function () {
  $('#button').click(function() {
    for(var i = 0; i < 5; i++) {
      $('#container').append('<input type="text" class="dynamic-input">')
    }
    //You're still within the scope of the callback, so this is the appropriate location
     $('.dynamic-input').on('keyup, function() {
        console.log($(this).val());
    });

  });

That's because you performed the jquery binding BEFORE the element is created. 那是因为您在创建元素之前执行了jquery绑定。 When the $('.dynamic-input').keyup is executed. 当执行$('.dynamic-input').keyup Your inputs, which are created AFTER you clicked the button, does not exist yet. 您单击按钮后创建的输入尚不存在。

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

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