【问题标题】:Get next datepicker and set the minimum date value获取下一个日期选择器并设置最小日期值
【发布时间】:2010-11-23 10:21:41
【问题描述】:

我有一系列日期选择器,每个选择器都分配了一个类。我想要实现的是检测(使用)日期选择器 onSelect() 处理程序下一个日期选择器并将最小日期设置为所选日期加一天。

我尝试过使用 $(this).next('.publication_date);但这似乎没有返回任何东西。

事实上,在选定的日期选择器之后可能会有一系列日期选择器,因此我需要单独设置每个日期选择器。

标记:-

$(".publication_date").livequery(function() {
 $(this).datepicker({
  dateFormat: "dd M yy",
  changeYear: true,
  changeMonth: true,
  altFormat: 'yy-mm-dd',
  altField: $(this).next(),
  onSelect: function(dateText, inst) {
    console.log($(this).next('.publication_date'));
  }
});
});

示例 Html:-

<div id="tierRight">
    <div id="tier1" class="tier">
         <p class="title">Tier 1</p>
            <label class="publication_date_label" for="publication_date_1">Publication Date: </label>
            <input type="text" value="" readonly="readonly" name="tier[1][publication_date]" id="publication_date_1" size="10" maxlength="10" class="publication_date hasDatepicker">
            <input type="hidden" class="publication_date_db" value="2010-11-24" name="tier[1][publication_date_db]" id="publication_date_db_1">
            <img class="date_clear" src="delete_grey.gif" onclick="$(this).siblings('input').val('');">
        </div>
    <div id="tier2" class="tier">
        <p class="title">Tier 2 <a class="removeTier" title="" href="#">Remove</a></p>
        <label class="publication_date_label" for="tier[2][publication_date]">Publication Date: </label>
        <input type="text" value="" readonly="readonly" name="tier[2][publication_date]" id="publication_date_2" size="10" maxlength="10" class="publication_date hasDatepicker">&nbsp;
        <input type="hidden" class="publication_date_db" name="tier[2][publication_date_db]" id="publication_date_db_2" value="">
        <img src="delete_grey.gif" );="" ).val(="" input="" onclick="$(this).siblings(" class="date_clear">
    </div>
    <div id="tier3" class="tier">
        <p class="title">Tier 3 <a class="removeTier" title="" href="#">Remove</a></p>
        <label class="publication_date_label" for="tier[3][publication_date]">Publication Date: </label>
        <input type="text" value="" readonly="readonly" name="tier[3][publication_date]" id="publication_date_3" size="10" maxlength="10" class="publication_date hasDatepicker">&nbsp;
        <input type="hidden" class="publication_date_db" name="tier[3][publication_date_db]" id="publication_date_db_3" value="">
        <img src="/delete_grey.gif" );="" ).val(="" input="" onclick="$(this).siblings(" class="date_clear">
    </div>
</div>

尝试使用下面的代码,但由于某种原因,样式变得很奇怪:

$('#start_date_datepicker').datepicker({
                dateFormat: "dd M yy",
                altField: '#start_date_datepicker_altfield',
                altFormat: 'yy-mm-dd',
                changeYear: true,
                changeMonth: true,
                minDate: new Date(),
                onSelect: function(dateText, inst) {
                    $('.publication_date').each(function(){
                        var current_tier = $(this).closest('.tier');
                        //Do all your stuff here
                        console.log(current_tier);
                        $(current_tier).datepicker({
                            minDate: new Date()
                        });
                    });
                }
            });

【问题讨论】:

  • 我们需要查看您的标记来说明如何相对获取它。
  • 尼克,我添加了一些示例代码。

标签: jquery datepicker


【解决方案1】:

在这种情况下,您需要使用 .closest() 向上到达父级 .tier,然后到达 .next() 兄弟级,然后是 .find() 内部的 .publication_date 控件,如下所示:

$(".publication_date").livequery(function() {
 $(this).datepicker({
  dateFormat: "dd M yy",
  changeYear: true,
  changeMonth: true,
  altFormat: 'yy-mm-dd',
  altField: $(this).next(),
  onSelect: function(dateText, inst) {
    console.log($(this).closest(".tier").next().find('.publication_date'));
  }
 });
});

检查你的 HTML,那里有很多错误,随机的 JavaScript 片段会给你带来问题。

【讨论】:

  • 啊那是因为我是从 Firebug 复制过来的。
  • @user275074 - 这不会改变答案,无效的 HTML 只是一个评论:)
【解决方案2】:
$('.publication_date').each(function(){

      var current_tier = $(this).closest('.tier');

      //Do all your stuff here

});

【讨论】:

  • 我不确定这应该做什么......但它不是有效的 JavaScript,.hasClass() 返回一个布尔值。
  • 我可以看到这会给我所有的 datePickers 但我如何单独设置 minDate?
  • 问题。每个日期选择器的 minDate 是否会有所不同?
  • 是的,minDate 需要提前 1 天。
  • @user.. :然后使用 .prev('.tier') 访问先前匹配的元素,或者使用 each(function(){...}) 的全局日期跟踪变量
猜你喜欢
  • 1970-01-01
  • 2019-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-31
  • 1970-01-01
相关资源
最近更新 更多