简体   繁体   中英

Assigning datepicker to array of input fields

i've looked around the internet but can't seem to find what i need.

I have an input field called date[] with class 'datepicker', i then have a function to clone these input fields using jquery, so i can have multiple fields

$(".datepicker").datepicker({dateFormat: 'dd-mm-yy', minDate: 0});

<input type="text" name="date[]" value="" class="datepicker" />

When there is one input field on the page, it works fine, when there are 2, they are both populated with the same value, the picker doesnt show on the first when you click it, but it does on the second, however the value doesnt get changed when it should.

Any ideas how to get this working

EDIT:

After playing around a bit more, this code does work for multiple fields, but only when they are static. My input fields are generated dynamically using .clone() and .prependTo(), this is when the problems start. Any ideas?

in the line $(".datepicker").datepicker({dateFormat: 'dd-mm-yy', minDate: 0}); , .datepicker is like an argument to datepicker indicating where to put returned values. And it should be unique. If you use more than one element having class as datepicker , datepicker function will return same value to both. Instead you can use something like -

$("#datepicker1,#datepicker2").datepicker({dateFormat: 'dd-mm-yy', minDate: 0});​ and html is - ​Demo

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