2013-11-23 94 views
0

我想在点击一个小图片时选择一个日期选择器,选择日期,使用所选日期根据所选日期填充一组年份,月份和日期的选定元素。我想出了onSelect部分,并将所选日期应用于select元素中的选项。隐藏jQuery日期选择器

我的问题是datepicker没有关闭日期选择。它停留在那里。如果我调用hide()方法,它也隐藏图像(span元素的一部分)。这是我的HTML代码:

<span id="pickupCalendar"><img src="/images/calendar.png"></img></span> 

这是脚本:

$("#pickupCalendar").click(function() { 
    $("#pickupCalendar").datepicker({ 
     dateFormat: 'dd-mm-yy', 
     onSelect: function (dateText, inst) { 
      //code to fill the other elements with the selected date goes here 
      $("#pickupCalendar").datepicker().hide(); //<-- hides the image (span element along with the datepicker) 
     } 
    }); 
}); 
+0

为什么你的跨度内的图像?你为什么要在其点击处理程序中定义日期选择器?你能提供一个关于jsfiddle的演示吗? –

+0

因为我明白日期选择器可以附加到span元素。这里是我用一个按钮替换图像的小提琴。 http://jsfiddle.net/Ysxv3/ – Chinmay

回答

1

我会更改HTML标记,因此它更容易定位元素。

<span id="pickupCalendar"></span><button>Button</button> 

    $("button").click(function() { 
     $("#pickupCalendar").show(); 
     $("#pickupCalendar").datepicker({ 
      dateFormat: 'dd-mm-yy', 
      onSelect: function (dateText, inst) { 
       //code to fill the other elements with the selected date goes here 
       $("#pickupCalendar").datepicker().hide(); 
      } 
     }); 
    }); 

FIDDLE

+0

非常好!我修改了您的版本,但没有创建后续的日期选择器。这是我们的解决方案:http://jsfiddle.net/Ysxv3/4/刚刚看到你做出了改变。感谢您的指导! :) – Chinmay

+0

是的,只需将概念适应于您的环境!很高兴我能帮上忙。 – Sebsemillia

+0

我仍然想知道为什么元素被隐藏。 “摧毁”的作用类似。 – Chinmay

0

你可以试试这个?

$("#pickupCalendar").click(function() { 
    $("#pickupCalendar").datepicker({ 
     dateFormat: 'dd-mm-yy', 
     onSelect: function (dateText, inst) { 
      //code to fill the other elements with the selected date goes here 
      $("#pickupCalendar").datepicker().hide(); 
      $("#pickupCalendar img").hide(); 
     } 
    }); 
}); 
+0

嗯,这将无法正常工作,它不工作。请注意小提琴。 – Chinmay