简体   繁体   中英

Add or remove a datepicker from a textfield according to selected dropdown value

I have a form used to search data. On that form I have a textbox , a dropdown list , and a search button . In my dropdown , I have options:

  • check in date
  • last name
  • reservation id

I want to add a datepicker to the textbox , but only when "check in date" has been selected.

Once the datepicker is added to the textbox - I can't remove it. How can I remove the datepicker when the other options have been selected?

This is my jquery

$('#list').change(function() {
  var selected = $(this).val();

  if(selected == 2) {
    $('#text').datepicker({ dateFormat: 'yy-mm-dd' });
  } else {
    $('#text').prev().val(''); //here , need to remove the date picker 
  } 
});

Ok when the another option is selected you can use the .datepicker( "destroy" ) function to remove it.

example:

if(selected == 2) {
    $('#text').datepicker({ dateFormat: 'yy-mm-dd' });
  } else {
    $('#text').prev().val('');

    // first test if it exist...example you can set a property then if true destroy it.
    $('#text').datepicker("destroy");
  } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM