Ash*_*her 5 javascript jquery ruby-on-rails timepicker twitter-bootstrap
我正在尝试使用getTime函数从Bootstrap Timepicker获取时间,但只有在按下按钮时才会在控制台中收到错误消息(带有我的按钮的ID,请参阅下面的代码.)
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>
Run Code Online (Sandbox Code Playgroud)
JavaScript的
<script type="text/javascript">
$('#my-button').on('click', function() {
alert($('.timepicker').getTime());
});
$('.timepicker').timepicker({
template : 'dropdown',
showInputs: false,
showSeconds: false
});
</script>
Run Code Online (Sandbox Code Playgroud)
控制台出错
Uncaught TypeError: Object [object Object] has no method 'getTime'
Run Code Online (Sandbox Code Playgroud)
我一直试图弄清楚这几个小时.我以为我可以使用源代码来学习如何使用选择器,它看起来像是一个getTime()函数,但我不能为我的生活弄清楚如何使用它.我正在使用Rails,因为它的价值.多谢你们!
pro*_*gus 13
FWIW可以调用,getTime()但不能直接调用它,因为$('.timepicker')从DOM返回<input>元素,而不是实际的timepicker对象.
文档中没有提到它,但是如果你看一下bootstrap-timepicker.js源代码,那么绑定函数($.fn.timepicker)会使用jQuery的data()功能存储对DOM元素的实际timepicker对象的引用.
所以,这有效:
$('.timepicker').data("timepicker").getTime();
Run Code Online (Sandbox Code Playgroud)
(当然,getTime()仍然只返回一个字符串,所以在这种情况下,父母使用val()的建议可能更干净,除非你认为可能有无效的数据输入到字段中,并且还没有被timepicker清理.)
Ash*_*mar 11
$('#ReportingTime').data('timepicker').hour;
$('#ReportingTime').data('timepicker').minute;
$('#ReportingTime').data('timepicker').meridian;
Run Code Online (Sandbox Code Playgroud)
那不是一个公共函数,方法getTime仅对该函数私有。意味着您无法访问它。
如果您想获取时间,请执行a .val();并对其进行解析。或者,您可以修改脚本以公开.getTime();功能。
| 归档时间: |
|
| 查看次数: |
22704 次 |
| 最近记录: |