Javascript 从 Bootstrap Timepicker 获取时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11573140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting the time from a Bootstrap Timepicker
提问by Asher Walther
I'm trying to get the time from a Bootstrap Timepickerusing the getTimefunction, but only get an error message in the console when I press the button (with ID of my-button, see code below.)
我正在尝试使用getTime函数从Bootstrap Timepicker获取时间,但是当我按下按钮时,只会在控制台中收到一条错误消息(带有 my-button 的 ID,请参阅下面的代码。)
HTML
HTML
<div class="input-append bootstrap-timepicker-component">
<input type="text" class="timepicker input-small">
<span class="add-on">
<i class="icon-time"></i>
</span>
</div>
JavaScript
JavaScript
<script type="text/javascript">
$('#my-button').on('click', function() {
alert($('.timepicker').getTime());
});
$('.timepicker').timepicker({
template : 'dropdown',
showInputs: false,
showSeconds: false
});
</script>
Error in the console
控制台中的错误
Uncaught TypeError: Object [object Object] has no method 'getTime'
I've been trying to figure this out for hours. I thought I could use the source code to learn how to use the picker, and it looks like there's a getTime() function, but I can't for the life of me figure out how to use it. I'm using Rails, for what it's worth. Thanks guys!
几个小时以来,我一直试图弄清楚这一点。我想我可以使用源代码来学习如何使用选择器,看起来有一个 getTime() 函数,但我终生无法弄清楚如何使用它。我正在使用 Rails,因为它物有所值。谢谢你们!
采纳答案by Steven
That's not a public function, the method getTime is private to just that function. Meaning you can't access it.
这不是一个公共函数,方法 getTime 只是该函数的私有函数。这意味着您无法访问它。
If you want to get the time just do a .val();
and parse it. Or you could modify the script to expose .getTime();
function.
如果你想得到时间,只需做一个.val();
并解析它。或者您可以修改脚本以公开.getTime();
功能。
回答by projectgus
FWIW it is possible to call getTime()
but it can't be called directly because $('.timepicker')
returns the <input> element from the DOM, not the actual timepicker object.
FWIW 可以调用,getTime()
但不能直接调用,因为$('.timepicker')
从 DOM 返回 <input> 元素,而不是实际的 timepicker 对象。
It's not mentioned in the documentation, but if you look at the bootstrap-timepicker.js source then the binding function ($.fn.timepicker
) stores a reference to the actual timepicker object against the DOM element, using jQuery's data() feature.
文档中没有提到它,但是如果您查看 bootstrap-timepicker.js 源代码,则绑定函数 ( $.fn.timepicker
) 使用 jQuery 的 data() 功能针对 DOM 元素存储对实际 timepicker 对象的引用。
So, this works:
所以,这有效:
$('.timepicker').data("timepicker").getTime();
(Of course getTime()
still just returns a string, so in this case the parent's suggestion of using val() is probably cleaner unless you think there might be invalid data typed into the field, and not yet cleaned by the timepicker.)
(当然getTime()
仍然只返回一个字符串,所以在这种情况下,父母使用 val() 的建议可能更清晰,除非您认为可能在字段中输入了无效数据,并且时间选择器尚未清除。)
回答by Ashish Kumar
$('#ReportingTime').data('timepicker').hour;
$('#ReportingTime').data('timepicker').minute;
$('#ReportingTime').data('timepicker').meridian;