将 jQuery 日期选择器应用于多个实例 [英] Apply jQuery datepicker to multiple instances
问题描述
我有一个 jQuery 日期选择器控件,它可以在一次实例中正常工作,但我不确定如何让它为多个实例工作.
I've got a jQuery date picker control that works fine for once instance, but I'm not sure how to get it to work for multiple instances.
<script type="text/javascript">
$(function() {
$('#my_date').datepicker();
});
</script>
<% Using Html.BeginForm()%>
<% For Each item In Model.MyRecords%>
<%=Html.TextBox("my_date")%> <br/>
<% Next%>
<% End Using%>
没有 For Each 循环,它工作正常,但如果MyRecords"集合中有多个项目,则只有第一个文本框获得日期选择器(这是有道理的,因为它与 ID 相关联).我尝试为文本框分配一个类并指定:
Without the For Each loop, it works fine, but if there's more than one item in the "MyRecords" collection, then only the first text box gets a date picker (which makes sense since it's tied to the ID). I tried assigning a class to the text box and specifying:
$('.my_class').datepicker();
但是虽然到处都显示了日期选择器,但它们都会更新第一个文本框.
but while that shows a date picker everywhere, they all update the first text box.
完成这项工作的正确方法是什么?
What is the right way to make this work?
推荐答案
html:
<input type="text" class="datepick" id="date_1" />
<input type="text" class="datepick" id="date_2" />
<input type="text" class="datepick" id="date_3" />
脚本:
$('.datepick').each(function(){
$(this).datepicker();
});
(为了简单起见,进行了一些伪编码)
(pseudo coded up a bit to keep it simpler)
这篇关于将 jQuery 日期选择器应用于多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!