Rebuild a clear button for the datepicker control in jquery UI

Source: Internet
Author: User

If you encounter the same problem when the time is tight and the task is heavyProgramMember brother, please read it directlyCode.

Recently it almost became a javascript front-end programmer... Originally, I wanted to focus only on the background ...... No way. Work is required ~

Today, a colleague suddenly ran over and asked me if the datepicker in jquery UI could add a clear button to clear it with one click. I think the powerful datepicker must have taken this requirement into consideration, maybe it was a simple property setting thing. As a result, I was disappointed. datepicker didn't provide the clear function. Google had a chat and concluded that it may have been, but it was removed for some reason from a certain time and never came back again. Some people on the internet gave some severe hack practices and added a clear button to the HTML function generated by the control. This is not an act for JavaScript cainiao of my generation, even if it is just copy & paste. in fact, datepicker provides two buttons, today and done. Why don't we start with this done button to make it clear and transform it into a clear button, at the same time, try to patch only the peripheral components of the control? After analyzing the HTML generated by datepicker, it seems that it is not difficult. You only need to use the following code to solve the problem.

 1 $ (Document). Ready ( Function ()
2 {
3 // It is used to store references to the date text box currently being operated.
4 VaR Datepicker_currentinput;
5
6 // Set Default datepicker settings
7 $. Datepicker. setdefaults ({showbuttonpanel: True , Closetext: 'clear', beforeshow: Function (Input, insT) {datepicker_currentinput = input ;}});
8
9 // Bind the Click Event of the "done" button. When triggered, clear the value of the text box.
10 $ (". UI-datepicker-Close"). Live ("click ", Function ()
11 {
12 Datepicker_currentinput.value = "";
13 });
14 });

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.