简体   繁体   English

将Javascript日期选择器转换为文本字段

[英]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 我正在尝试为日期字段使用日期选择器,但我的日期字段ID是动态ID,因为这些字段在表格行中生成,例如

<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, 但是我的日期选择器脚本需要ID才能将日期选择器附加到上面的文本字段中,如下所示,

<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 并从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 最好使用一个类而不是多个id。您可以根据类的索引号来区分文本字段值

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/ js小提琴链接: http : //jsfiddle.net/sarath704/LhPP2/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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