Grails 和 Jquery 日期选择器
Posted
技术标签:
【中文标题】Grails 和 Jquery 日期选择器【英文标题】:Grails and Jquery Datepicker 【发布时间】:2016-09-11 09:47:51 【问题描述】:我想编写一个代码,允许我创建主题、购买日期和资产价值等输入,输入框具有属性“class= “datepicker””,起初一切正常,但当我调用作品追加以在页面中添加其他输入(与上述相同)时,“datepicker”不再给出任何生命迹象。 我给你看代码:
<div class="well bs-component" ><fieldset><div id="elencoBeni">
<div class="input-group">
<span class="input-group-addon">Oggetto</span>
<g:textField class="form-control" placeholder="Oggetto" name="oggetto" id="oggetto" />
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar" ></span></span>
<g:textField class="form-control datepicker" placeholder="Data acquisto" name="dataAcquisto" id="dataAcquisto" />
<span class="input-group-addon">Valore</span>
<g:textField class="form-control" placeholder="Valore" name="valoreBene" id="valoreBene"/><br/>
</div><br/>
</div>
<label class="btn btn-success" id="aggiungiOggetto" name="aggiungiOggetto" >+</label>
</fieldset></div>
jquery函数:
<g:javascript>
$(document).ready(function()
$('.datepicker').datepicker(
language: 'it'
); var i = 0;
$("#aggiungiOggetto").click(function()
$("#elencoBeni").append('<div class="input-group"><span class="input-group-addon">Oggetto</span><input type="text" class="form-control" placeholder="Oggetto" name="oggetto'+i+'" id="oggetto'+i+'"/><span class="input-group-addon"><span class="glyphicon glyphicon-calendar" ></span></span><input type="text" class="datepicker form-control" placeholder="Data acquisto" name="dataAcquisto'+i+'" id="dataAcquisto'+i+'"/><span class="input-group-addon">Valore</span><input type="text" class="form-control" placeholder="Valore" name="valoreBene'+i+'" id="valoreBene'+i+'"/><br/></div><br/>');
i++
); </g:javascript>
【问题讨论】:
【参考方案1】:由于输入是动态添加到dom的,所以添加到dom后需要为元素初始化datepicker。
$(document).ready(function()
$('.datepicker').datepicker(
language: 'it'
);
var i = 0;
$("#aggiungiOggetto").click(function()
$("#elencoBeni").append('<div class="input-group"><span class="input-group-addon">Oggetto</span><input type="text" class="form-control" placeholder="Oggetto" name="oggetto' + i + '" id="oggetto' + i + '"/><span class="input-group-addon"><span class="glyphicon glyphicon-calendar" ></span></span><input type="text" class="datepicker form-control" placeholder="Data acquisto" name="dataAcquisto' + i + '" id="dataAcquisto' + i + '"/><span class="input-group-addon">Valore</span><input type="text" class="form-control" placeholder="Valore" name="valoreBene' + i + '" id="valoreBene' + i + '"/><br/></div><br/>');
// initialize datepicker again, which includes the dynamically added element
$('.datepicker').datepicker(
language: 'it'
);
i++
);
);
【讨论】:
【参考方案2】:因为新元素没有绑定.datepicker()
。
代替:
$('.datepicker').datepicker(
language: 'it'
);
使用:
$('body').on('focus',".datepicker", function()
$(this).datepicker(
language: 'it'
);
);
【讨论】:
以上是关于Grails 和 Jquery 日期选择器的主要内容,如果未能解决你的问题,请参考以下文章