[英]Javascript function not working when the event is called by element from another function
I've downloaded a calendar script, that when the input element is click a calendar will popup. 我已经下载了一个日历脚本,当单击输入元素时,将弹出一个日历。
<script language="JavaScript">
$(function() {
$( ".datepicker" ).datepicker();
});
</script>
<input class='datepicker' />
The above code works fine. 上面的代码工作正常。
But when the element is coming from a javascript... 但是当元素来自JavaScript时...
<script language="JavaScript">
function showElement()
{
$('#mydiv').html("<input class='datepicker' />");
}
</script>
<div id='mydiv'></div>
The calendar don't shows up as the input is clicked. 单击输入后,日历不会显示。 I think there is no error in the downloaded script.
我认为下载的脚本没有错误。 I think I need to do something to make this calendar appears even if the element is created by a javascript.
我认为即使元素是由javascript创建的,我也需要做些使该日历显示的操作。 What should I modify?
我应该修改什么? Thanks!
谢谢!
Change your function to: 将功能更改为:
function showElement()
{
$('#mydiv').html("<input class='datepicker' />");
$( ".datepicker" ).datepicker();
}
Or even better, do the following: 甚至更好,请执行以下操作:
function showDatePicker(selector) {
$(selector).datepicker();
}
function showElement()
{
$('#mydiv').html("<input class='datepicker' />");
showDatePicker(".datepicker");
}
This way you can show the date picker whenever you want by calling the showDatePicker()
function for any element that matches the selector you pass as a parameter to the function. 这样,您可以通过对与作为参数传递给函数的选择器匹配的任何元素调用
showDatePicker()
函数,随时显示日期选择器。 Of course in your case you would only want to call it after you have called the showElement()
function 当然,在您的情况下,您只想在调用
showElement()
函数之后才调用它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.