I've got a datepicker in an asp.net web app that has today's date set by default. We have an icon that when clicked needs to clear this date (and potentially add 'dd/mm/yyyy' as text back in as some descriptive text on date entry). I've added an icon and created a c开发者_开发百科lick event handler on it but I'm having issues setting the date to nothing.
$('#dt-clr').click( function(){
$(this).closest(".datepicker").val('dd/mm/yyyy');
});
I've even tried 'destroy' in the event handler above and ('setDate', '') but if that's the best way I mustn't be referencing it correctly.
Any ideas? :s
UPDATE: Eventually realised my icon was on the same DOM level as the datepicker so closest wasn't working (Gaby was onto this too, see comments below). So my code:
$(this).siblings(".datepicker").datepicker( 'setDate' , null )
...strips out the date and leaves me with a blank date textbox. Just need to figure out how to add the string 'dd/mm/yyyy' in instead of my empty box.
UPDATE 2: Ok, this works. There's probably a better way, if there is please let me know:
$(this).siblings(".datepicker").datepicker( 'setDate' , null );
$(this).siblings(".datepicker").val('dd/mm/yyyy');
Have you tried
$(this).closest(".datepicker").datepicker( 'setDate' , null )
http://docs.jquery.com/UI/Datepicker#method-setDate
[Update] (after your comment)
Have a look at this example i made : http://jsbin.com/obudu/4
Works just fine for all browsers checked...
I just noticed that you use the .closest
method of jQuery. Most likely that is what is causing the problem.. The $(this).closest(..)
must not be returning anything.. closest
goes up the DOM tree, which means that your datepicker object must be a parent of icon..
Give some code about the html structure. Where is the icon in relation to the datepicker..
精彩评论