简体   繁体   English

jQuery-在动态创建的元素上单击(使用“ .on()”)无效,只能双击。 为什么?

[英]jQuery - click (with using “.on()”) on a dynamically created element doesn't work, only double-click. Why?

I have a button that after clicking on it generates a new input (or more inputs). 我有一个按钮,单击它后会生成一个新的输入(或多个输入)。 When I click into the newly generated input(s), I want to pop up a calendar widget. 当我单击新生成的输入时,我想弹出一个日历小部件。

I am doing it this way: 我这样做是这样的:

$('#datetime_inputs').on('click', '.new_datetime_input', function() {
  $(this).datetimepicker({
    format: 'Y/m/d H:i'
  });
});

When I click into this new input, nothing happens. 当我单击此新输入时,没有任何反应。 When I click into that 2nd time, the widget is loaded (good). 当我第二次单击时,将加载小部件(良好)。 If I click there 3rd time, the widget is loaded (good), 4th time - the widget is loaded (good) and so on. 如果我第三次单击那里,则加载小部件(良好),第四次-加载小部件(良好),依此类推。

Why when I click there the first time the widget is not loaded? 为什么当我第一次单击该窗口小部件时未加载它?

Thank you. 谢谢。

EDIT: The plugin I use link . 编辑:我使用link的插件。

Change your code to: 将您的代码更改为:

$('.new_datetime_input').datetimepicker({
    format: 'Y/m/d H:i'
});

Currently, you are initializing it on the first click, which is why nothing seems to happen. 当前,您是在第一次点击时对其进行初始化,这就是为什么似乎什么都没有发生的原因。

You do not need to set and handle a click event to display it, it will be done by the plugin after calling the method as above. 您无需设置和处理click事件即可显示它,该事件将在调用上述方法后由插件完成。


EDIT 编辑

To initialize and open the datepicker on dynamically-added elements, you could do this: 要初始化并打开动态添加的元素上的日期选择器,可以执行以下操作:

$('#datetime_inputs').on('click', '.new_datetime_input', function() {

  // Do not initialize twice on same element
  if($(this).hasClass('init')) return;
  else $(this).addClass('init');

  // Initialize
  $(this).datetimepicker({
    format: 'Y/m/d H:i'
  })
  $(this).click(); // Open datetimepicker
});

1.Problem : The widget is not bounded to element, beforehand. 1.问题:窗口小部件没有预先绑定到元素。

  1. Consequence: When you click first time, the widget code is mapped to element. 结果:第一次单击时,小部件代码将映射到元素。

3 Solution: you need to initialize/map/bound the element before you make click event. 3解决方案:创建点击事件之前,需要初始化/映射/绑定元素。 For say on page load. 对于说在页面加载。

eg. 例如。

 function bindDatePicker(i){
                $("#datepick"+i).datepicker({
              changeMonth: true,
                  changeYear: true,
                  dateFormat : 'dd/mm/yy',
                  numberOfMonths:1,
                 maxDate: "+0M +0D",yearRange:"1960:2099"
                  });       
        };

if I have this function to bind a datepicker with an element with id 'datepickX', 如果我有此功能将日期选择器与ID为'datepickX'的元素绑定,

<input type="text" maxlength="12" onclick="bindDatePicker($index);" id="datepick4" >

I will call this function on page load, and it will bind the widget with element. 我将在页面加载时调用此函数,它将把小部件与element绑定。 So next time you will click on elemtn with some Magic !!! 因此,下次您将单击带有一些魔术的elemtn! It will open up in first time only. 它将仅在第一次打开。

This happens because on "click" you are initializing the datetimepicker. 发生这种情况是因为在“单击”时,您正在初始化datetimepicker。 So the next time you will click it, it will be already initialized and will show the calendar to you. 因此,下次您单击它时,它将已经初始化并向您显示日历。 Just go with Jai's suggestion and initialize it during creation. 只需遵循Jai的建议并在创建过程中对其进行初始化。 It will have it's on click event, so you don't need to set it manually. 它将具有单击事件,因此您无需手动设置。

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

相关问题 jQuery click事件不适用于动态创建的表单字段 - jQuery click event doesn't work for dynamically created form field 使用jQuery .on()方法不适用于动态创建的元素 - Using jQuery .on() method doesn't work on dynamically created element 为什么此点击处理程序不能在JQuery中工作? - Why doesn't this click handler work in JQuery? Jquery单击事件未在使用jquery动态创建的元素上触发 - Jquery click event not firing on the element created dynamically using jquery JQUERY 单击事件仅在第一次单击调用时不起作用 - JQUERY Click event doesn't work only at the first click call 如何使用jQuery将点击侦听器附加到动态创建的子元素上? - How to attach click listener to a dynamically created child element using jQuery? jQuery侦听动态创建的元素上的click事件 - jquery listen to click event on dynamically created element 将元素放入数组时出现问题,使用分离和 append 到另一个使用 jquery 的元素单击不起作用 - Problem when put a element in array, using detach and append to another element using jquery click doesn't work .focus()不适用于jQuery动态创建的元素 - .focus() doesn't work on element created by jQuery dynamically jQuery,在点击它的回调中操作动态创建的元素 - jQuery, manipulate dynamically created element in callback of a click on it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM