简体   繁体   中英

Javascript Date Picker Into A Textfield

I'm trying to use a date picker for a date field but my date fields ID is a dynamic id, since the fields generates in table rows, like

<input type="text" id="date<%=n%>"/>

But my date picker script requires the ID to attach the date picker into the above text field like below,

<script type="text/javascript">
        new datepickr('datepick', { dateFormat: 'Y-m-d' });
    </script>

Is there any other way to make this work?

Add a class to your input field

<input type="text" id="date<%=n%>" class="datepicker"/>

and get its value from javascript

var date = $('.datepicker').datepicker();

Better to use one class instead of multiple id's .you can differentiate the text filed value based on index number of class

first:<input type="text" class="date">
second:<input type="text" class="date">
third:<input type="text" class="date">

$(document).ready(function(){
   $('.date').datepicker(); 
    $('.date').on('change',function(){
       var index=$('.date') .index(this);
       $('.date:eq('+index+')').val();        
    });
});

js fiddle link: http://jsfiddle.net/sarath704/LhPP2/1/

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