jQuery on.click不适用于首次点击(带有动态元素) [英] jquery on.click doesn't work on first click (with a dynamic element)
本文介绍了jQuery on.click不适用于首次点击(带有动态元素)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在努力解决以下问题:动态创建输入元素,并尝试将datepicker附加到它.但是,仅当我在输入元素外部的某个位置单击然后再次在输入上单击时,它才起作用.控制台日志在第一次单击时工作正常,所以我不知道这里出了什么问题?
I'm struggling with the following problem: I dynamically create input elements and I try to attach a datepicker to it. But it works only when I click somewhere outside the input element and then on the input again. Console log is working fine on first click, so I don't know what is the issue here?
$(document).ready(function(){
$("div.single_condition").on("click", "input", function(){
if($("select.select_operator").val() == '>'){
console.log("O.K.");
$(this).datepicker();
}
else {
console.log("NOT O.K.");
}
});
});
推荐答案
即使使用.datepicker()
将日期选择器正确绑定到输入中,也需要在第一次单击时显示它.
Even if you bind a datepicker to your input correctly using .datepicker()
, you need to show it on first click.
尝试此代码,
$(this).datepicker().datepicker("show");
演示
更改了JS :
$("div.single_condition").on("click", "input", function () {
if ($("select.select_operator").val() == '>') {
console.log("O.K.");
$(this).datepicker().datepicker("show");
} else {
console.log("NOT O.K.");
}
});
建议 :在附加动态元素之后而不是在click
上绑定.datepicker()
.
这篇关于jQuery on.click不适用于首次点击(带有动态元素)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文