将jQuery datepicker应用于多个实例 [英] Apply jQuery datepicker to multiple instances

查看:554
本文介绍了将jQuery datepicker应用于多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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();
});

(伪编码以保持简单)

这篇关于将jQuery datepicker应用于多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆