最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change dropdown select option depend on datepicker select - Stack Overflow

programmeradmin2浏览0评论

I am trying to create a search that has as option to choose a day.

I want to give that option with drop-down select and with a calendar as well.

I am using Bootstrap and JQuery-UI datepicker.

My code is working, both of options are working BUT

I want when user select a day on calendar to change dynamically the date on dropdown as well.

Here is my code:

<div class="input-group col-lg-2 col-md-12 col-sm-12 search">
      <label class="bd-form-label" style="float: left;width: 100%;">Dates</label>
         <select name="name" id="name_id" style="width: 80%;float: left;">
            <option value="">Any Month</option>
            <option value="October 2017">October 2017</option>
            <option value="November 2017">November 2017</option>
            <option value="December 2017">December 2017</option>
            <option value="January 2018">January 2018</option>
         </select>
         <input type="hidden" id="datepicker" class="hasDatepicker" value="16-Oct-2017" style="">
            <img class="ui-datepicker-trigger" src=".gif" alt="..." title="...">
     </div>

I am trying to create a search that has as option to choose a day.

I want to give that option with drop-down select and with a calendar as well.

I am using Bootstrap and JQuery-UI datepicker.

My code is working, both of options are working BUT

I want when user select a day on calendar to change dynamically the date on dropdown as well.

Here is my code:

<div class="input-group col-lg-2 col-md-12 col-sm-12 search">
      <label class="bd-form-label" style="float: left;width: 100%;">Dates</label>
         <select name="name" id="name_id" style="width: 80%;float: left;">
            <option value="">Any Month</option>
            <option value="October 2017">October 2017</option>
            <option value="November 2017">November 2017</option>
            <option value="December 2017">December 2017</option>
            <option value="January 2018">January 2018</option>
         </select>
         <input type="hidden" id="datepicker" class="hasDatepicker" value="16-Oct-2017" style="">
            <img class="ui-datepicker-trigger" src="http://jqueryui./resources/demos/datepicker/images/calendar.gif" alt="..." title="...">
     </div>

e.g If user click on calendar October 2019 select option to display Oct 2019, because I have my input as <input type="hidden" />.

Is that possible or should I just prefer have one of these?

Share Improve this question edited Nov 12, 2020 at 13:23 peterh 1 asked Oct 16, 2017 at 12:01 MariaMaria 7602 gold badges9 silver badges23 bronze badges 2
  • So, you want: If user select 23-Oct 2017, Dropdown selected option should be October 2017? – Mayank Patel Commented Oct 16, 2017 at 12:05
  • @MayankPatel yes that's right. And even better if I could add another option with day in front of month and change both of them – Maria Commented Oct 16, 2017 at 12:07
Add a ment  | 

2 Answers 2

Reset to default 4

It can be done by formatting the date output format ($( "#datepicker" ).datepicker( "option", "dateFormat", "MM yy" );) and then setting the drop down value($('#name_id').val($('#datepicker').val());). Try running below code snippet

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Datepicker - Format date</title>
  <script src="https://code.jquery./jquery-1.12.4.js"></script>
  <script src="https://code.jquery./ui/1.12.1/jquery-ui.js"></script>
  <script>
  $( function() {
    $("#datepicker").datepicker();
	  //Below line will set format for date "Month year" .Note This format is custom,according to the drop down value
      $( "#datepicker" ).datepicker( "option", "dateFormat", "MM yy" );
	  //On change of Date from date picker, this will set value in select dropdown
	  $('#datepicker').change(function(){
			$('#name_id').val($('#datepicker').val());
        });
  } );
  
 
  </script>
</head>
<body>
 
<p>Calendar: <input type="text" id="datepicker" size="30"></p>
 

<div class="input-group col-lg-2 col-md-12 col-sm-12 search">
      <label class="bd-form-label" style="float: left;width: 100%;">Dates</label>
         <select name="name" id="name_id" style="width: 80%;float: left;">
            <option value="">Any Month</option>
            <option value="October 2017">October 2017</option>
            <option value="November 2017">November 2017</option>
            <option value="December 2017">December 2017</option>
            <option value="January 2018">January 2018</option>
         </select>
</div>
 
 
</body>
</html>

Firstly, I want to tell you that I have used the Moment library for the Date interpretation.

You just need to include the below library link in your code(CDN).

https://cdnjs.cloudflare./ajax/libs/moment.js/2.15.1/moment.min.js.

Now, I have used the Jquery ui-Datepicker for the date picker initialization.In it, I have used the onSelect change event to get the selected date.

Then, I have used the moment to get the month and year in the format MMMM-YYYY.

And finally, I have set this option in the drop-down.I also check if option already exists, don't insert it in the dropdown

So, whatever date you will select, that month will be inserted into the drop-down and also selected.

Below is the fiddle link.

Fiddle link

Let me know if it fits your requirements.Thanks!

$('#date1').datepicker({
    changeMonth: true,
    changeYear: true,
    showButtonPanel: true,
    dateFormat: "mm/dd/yy",
    onSelect: function(dateText) {
    var monthObj=moment(dateText,"MM/DD/YYYY");
    var drop_option=monthObj.format('MMMM YYYY');
    if($("#name_id option[value='"+drop_option+"']").length == 0){
    	 $('#name_id').append( '<option value="'+drop_option+'">' + drop_option + '</option>');
    }
  	$("#name_id option[value='"+drop_option+"']").prop('selected', true);
  }
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis./ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.15.1/moment.min.js"></script>
<div class="input-group col-lg-2 col-md-12 col-sm-12 search">
      <label class="bd-form-label" style="float: left;width: 100%;">Dates</label>
         <select name="name" id="name_id" style="width: 80%;float: left;">
            <option value="">Any Month</option>
            <option value="October 2017">October 2017</option>
            <option value="November 2017">November 2017</option>
            <option value="December 2017">December 2017</option>
            <option value="January 2018">January 2018</option>
         </select>
         <input type="hidden" id="datepicker" class="hasDatepicker" value="16-Oct-2017" style="">
            <img class="ui-datepicker-trigger" src="http://jqueryui./resources/demos/datepicker/images/calendar.gif" alt="..." title="...">
     </div>
     <input type="text" id="date1" name="date1"/> <br/>

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>