首页 文章

多输入字段上的Jquery日期选择器

提问于
浏览
4

我有一个页面,其中有多个输入字段是fromdate和todate . 我创建了动态ID和名称 . 我使用过jquery日期选择器 .

enter image description here

码:

<?php
  $i=1;
  foreach($locations as $location)
  {

?>
<tr>
<td><?php echo $location['name'];?></td>
<td>
<input type="text" name="txtFromDate_<?php echo $location['pk_locationid'];?>" class="field" style="width:80px;" id="txtFromDate_<?php echo $i;?>"/>
</td>
    <td>
<input type="text" name="txtToDate_<?php echo $location['pk_locationid'];?>"  class="field" style="width:80px;" id="txtToDate_<?php echo $i;?>"/>
</td>
<td>
<input type="text" name="txtFromTime_<?php echo $location['pk_locationid'];?>"  class="time-pick field" style="width:80px;" />
</td>
<td>
<input type="text" name="txtToTime_<?php echo $location['pk_locationid'];?>"  class="time-pick field" style="width:80px;"/>
   </td>
   </tr>
   <?php
$i++;
    }
   ?>

日期选择器的Jquery代码:

(document).ready(function(){
        var count=0;
        count=<?php echo count($locations);?>;

        for(i=1;i<=count;i++)
        {
            var dates = $('#txtFromDate_'+i+', #txtToDate_'+i).datepicker({
            defaultDate: new Date(),
            changeMonth: true,
            changeYear: true,
            dateFormat: 'yy-mm-dd',
            onSelect: function(selectedDate) {
                var option = this.id == "txtFromDate_"+i ? "maxDate" : "minDate";
                var instance = $(this).data("datepicker");
                var date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
                dates.not(this).datepicker("option", option, date);
            }
    });
        }

显示日期和日期字段的Jquery日期选择器 . 在上面的代码中,我已经应用了从日期到日期的验证,即到日期应该大于从日期开始 .

上述代码的问题是此验证仅应用于最后一行 .

我希望这个验证应该应用于所有行 .

1 回答

  • 4

    您在循环的每次迭代中重新声明 dates 变量 . 因此,当循环完成时,您在onSelect处理程序中使用的 dates 变量等于循环中设置的最后一项 .

    Update 试试这段代码:

    Update2 还有一个问题是变量 i . 它的当前值在循环中可用,因此稍后,当使用onSelect处理程序时,i具有与上次迭代中一样的值 . 要解决这个问题,你必须将 i 放在另一个函数的上下文中,'s why I have wrapped code in the body of the loop in another function to which I' m传递 i 变量 .

    Update3 还有一件事 - 选择选项(minDate或maxDate)的逻辑必须颠倒过来 .

    $(document).ready(function(){
        var count=0;
        count=<?php echo count($locations);?>;
    
        for(i=1;i<=count;i++)
        {
            (function(i) {
                $('#txtFromDate_'+i+', #txtToDate_'+i).datepicker({
                    defaultDate: new Date(),
                    changeMonth: true,
                    changeYear: true,
                    dateFormat: 'yy-mm-dd',
                    onSelect: function(selectedDate) {
                        var option, otherPicker;
                        if (this.id == "txtFromDate_"+i) {
                           otherPicker = $("#txtToDate_"+i);
                           option = "minDate";
                        } else {
                           otherPicker = $("#txtFromDate_"+i);
                           option = "maxDate";
                        }
                        var instance = $(this).data("datepicker");
                        var date = $.datepicker.parseDate(instance.settings.dateFormat ||     $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
                        otherPicker.datepicker("option", option, date);
                    }
                };
            })(i);
        }
    });
    

相关问题