jQuery Timepicker插件-从事件方法调用时无法创建TimePicker元素 [英] JQuery Timepicker Addon - Can't create TimePicker elements while calling it from an event method
问题描述
trentrichardson的jQuery-Timepicker-Addon 有一个小问题.
Timepicker元素可以正确构建而不会出现问题,我可以根据文档进行所有操作.
Timepicker elements created within $(document).ready()
are built correctly without problems, and I can do everything from the documentation.
但是,当我尝试在按钮的onclick
JavaScript上创建新元素时,会引发错误:
However, when I try to create new elements on a button's onclick
JavaScript throws the error:
TypeError: $(...).timepicker is not a function
这是我的代码(以下所有代码块均在页面上的同一<script>
元素内):
Here's my code (all the following code blocks are inside the same <script>
element on the page):
$(document).ready(function () {
$('#hora').timepicker({
timeFormat: "hh:mm tt"
},$.timepicker.regional['es']); //works correctly, element is generated as desired
$('#origen_hora_confirmacion').timepicker({
timeFormat: "hh:mm tt"
},$.timepicker.regional['es']); //works correctly, element is generated as desired
});
稍后在同一<script>
元素中,我尝试在按钮的click事件上运行以下代码:
Later in the same <script>
element I try to run this code on a button's click event:
function agregar_vehiculo() {
// ...
$('#fechsalida0').timepicker({
timeFormat: "hh:mm tt"
}); //TypeError: $(...).timepicker is not a function
}
并在按钮上:
<input type="button" id="añadirVeh" name="añadirVeh" onclick="agregar_vehiculo()" value="Añadir Vehiculo">
这可能是什么原因?是插件问题,还是我缺少有关jQuery的内容?
What could be the cause of this? Is it the plugin problem or am I missing something about jQuery?
提前谢谢
推荐答案
如果$(document).ready
中的所有内容均按预期工作,则可以从javascript中添加事件侦听器:
If everything works as expected inside $(document).ready
you can add the event listener from the javascript:
$(document).ready(function () {
$('#añadirVeh').click(function () {
$('#fechsalida0').timepicker({
timeFormat: "hh:mm tt"
});
});
});
基本上重新编写$(document).ready
的agregar_vehiculo
函数内部.
Basically re-write your agregar_vehiculo
function inside of $(document).ready
.
这篇关于jQuery Timepicker插件-从事件方法调用时无法创建TimePicker元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!