我遇到了一个问题,我想创建多个时间字段,但是单击添加按钮就像添加动态输入字段一样。但是,当我在动态创建的输入字段上触发时间时,它将不起作用。请帮助我附上以下代码片段以供参考。
<div class="row slot-container">
<div class="input-field col s12">
<input type="text" placeholder="select date for demo" class="datepicker">
</div>
<div class="input-field col s6">
<input type="text" placeholder="from" class="timepicker">
</div>
<div class="input-field col s6">
<input type="text" placeholder="to" class="timepicker">
</div>
</div>
<a class="waves-effect waves-light btn addSlots">Add Slots</a>
$(function(){
$('.timepicker').pickatime({
default: 'now', // Set default time: 'now', '1:30AM', '16:30'
fromnow: 0,
twelvehour: true, // Use AM/PM or 24-hour format
donetext: 'OK', // text for done-button
cleartext: 'Clear', // text for clear-button
canceltext: 'Cancel', // Text for cancel-button,
container: 'body', // ex. 'body' will append picker to body
autoclose: false, // automatic close timepicker
ampmclickable: true, // make AM PM clickable
aftershow: function(){} //Function for after opening timepicker
});
$(document).on('click','.addSlots',function(){
$('.slot-container').after("<div class='row slot-container'><div class='input-field col s6'> <input type='text' placeholder='from' class='timepicker'> </div> <div class='input-field col s6'> <input type='text' placeholder='to' class='timepicker'> </div> </div>");
});
});
答案 0 :(得分:0)
仅更改第一行。 之前:
$(function(){
之后:
$(document).on('focus','.timepicker',function(){
答案 1 :(得分:0)
请尝试
$(function(){
$(document).off('click','.addSlots').on('click','.addSlots',function(){
$('.slot-container').after("<div class='row slot-container'><div class='input-field col s6'> <input type='text' placeholder='from' class='timepicker'> </div> <div class='input-field col s6'> <input type='text' placeholder='to' class='timepicker'> </div> </div>");
});
});