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

javascript - Date picker JS: how to have it work on multiple form fields - Stack Overflow

programmeradmin1浏览0评论

I am using this plugin with WP: /

The .js file includes this code:

jQuery(document).ready(function($) {
  $('#fieldName1').datepicker({
     autoclose: true
  });
});

Does anyone know how to doctor this bit of code so the date picker will function in multiple fields in the same form, such as fieldName2 and fieldName3 as well as fieldName1?

I thank you in advance for your efforts.

I am using this plugin with WP: http://wordpress/plugins/cf7-datepicker-alternative/

The .js file includes this code:

jQuery(document).ready(function($) {
  $('#fieldName1').datepicker({
     autoclose: true
  });
});

Does anyone know how to doctor this bit of code so the date picker will function in multiple fields in the same form, such as fieldName2 and fieldName3 as well as fieldName1?

I thank you in advance for your efforts.

Share Improve this question asked Aug 20, 2013 at 4:11 EricjtEricjt 1052 gold badges3 silver badges6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Instead of using id attribute, assign a meaningful class e.g. datepicker to the elements you want datepicker on. Then update your javascript to use that class selector.

Example:

<!-- HTML -->
<input type="text" id="fieldName1" class="datepicker" />
<input type="text" id="fieldName2" class="datepicker" />

// jQuery
jQuery(document).ready(function($) {
  $('.datepicker').datepicker({
     autoclose: true
  });
});

Update:

If you don't want to use class selectors and would want to use multiple ids, then you could do something like following:

<!-- HTML -->
<input type="text" id="fieldName1" />
<input type="text" id="fieldName2" />

// jQuery
jQuery(document).ready(function($) {
  $("#fieldName1, #fieldName2").each(function() {
     $(this).datepicker({
       autoclose: true
     });
  });
});

Instead of using an ID to identify your Input, use a class to identify multiple Inputs

<input id="fieldName1" class="datefield" />

$('.datefield').datepicker({
   autoclose: true
});
发布评论

评论列表(0)

  1. 暂无评论