简体   繁体   English

如何在2个不同的输入字段上调用相同的日期选择器

[英]How to call the same datepicker on 2 different input fields

I'm using Bootstrap Datepicker and I have a situation where I need to call the datepicker in the input field 2 or more times,but the problem is that,the datepicker opens on the first input filed,but on the rest it won't. 我正在使用Bootstrap Datepicker ,但我需要在输入字段中调用datepicker 2次或更多次,但问题是,datepicker在第一个输入的字段上打开,但在其余输入上则不会打开。 I tried changing the ID of the input and the datepicker script,still won't work. 我尝试更改输入的ID和datepicker脚本,但仍然无法正常工作。 Please help. 请帮忙。 Thx in advance 提前Thx

Code: 码:

<div class='form-group'>                                    
    <div class='row'>
        <div class="col-xs-12 date">
            <label>Label1</label>
            <div class="input-group input-append date" id="dateRangePicker">
                    <input id='input' type="text" value=' <?php echo $var; ?>' 
                    class="form-control" name="dateRangePicker" required />
                    <span class="input-group-addon add-on"><span class="glyphicon glyphicon-calendar"></span></span>
            </div><!-- end input group -->
        </div><!-- end col-xs-12 date --> 
    </div><!-- end row -->  

        <input type='checkbox'>
        <label>Data not available</label>
        </div><!-- end form-group -->

        <div class='form-group'>
            <label>Label2</label>
            <input id='input' type='text' class='form-control' value=' <?php echo $var;  ?>' name="dateRangePicker2" id="dateRangePicker2">


</div><!-- end form-group -->

SCRIPT 脚本
Note:I duplicated the code below,while i changed the id from dateRangePicker to dateRangePicker2,and it still wouldn't work. 注意:我复制了下面的代码,同时将id从dateRangePicker更改为dateRangePicker2,但仍然无法使用。

$(document).ready(function() {

    $('#dateRangePicker')
        .datepicker({
            format: 'dd.mm.yyyy',
            startDate: '01.01.2000',
            endDate: '31.12.2020'
        })
        .on('changeDate', function(e) {
            // Revalidate the date field
            $('#dateRangeForm').formValidation('revalidateField', 'date');
        });

    $('#dateRangeForm').formValidation({
        framework: 'bootstrap',
        icon: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            date: {
                validators: {
                    notEmpty: {
                        message: 'Message1'
                    },
                    date: {
                        format: 'dd.mm.yyyy',
                        min: '01.01.2000',
                        max: '31.12.2020',
                        message: 'Message2'
                    }
                }
            }
        }
    });


}); 

There are two issues in your HTML. HTML中有两个问题。 You are using attribute ID for two times. 您正在使用属性ID两次。 id="input" and one more id . id =“ input”和另外一个id。 Here below 在下面

  <input id='input' type='text' class='form-control' value=' <?php echo $var; ?>' name="dateRangePicker2" id="dateRangePicker2"> 

Try Below code 尝试以下代码

 $(document).ready(function() { $('#inputOne,#inputTwo') .datepicker({ format: 'dd.mm.yyyy', startDate: '01.01.2000', endDate: '31.12.2020', orientation: "top auto" }) .on('changeDate', function(e) { // Revalidate the date field $('#dateRangeForm').formValidation('revalidateField', 'date'); }); }); 
 <div class='row'> <div class="col-xs-12 date"> <label>Label1</label> <div class="input-group input-append date" id="dateRangePicker"> <input id='inputOne' type="text" class="form-control dateRangePicker2" name="dateRangePicker" required /> <span class="input-group-addon add-on"><span class="glyphicon glyphicon-calendar"></span></span> </div><!-- end input group --> </div><!-- end col-xs-12 date --> </div><!-- end row --> <div class='row'> <div class="col-xs-12 date"> <label>Label1</label> <div class="input-group input-append date" id="dateRangePicker"> <input id='inputTwo' type="text" class="form-control dateRangePicker2" name="dateRangePicker" required /> <span class="input-group-addon add-on"><span class="glyphicon glyphicon-calendar"></span></span> </div><!-- end input group --> </div><!-- end col-xs-12 date --> </div><!-- end row --> 

Working fiddle http://jsfiddle.net/anandgh/ajjLyd57/ 工作小提琴http://jsfiddle.net/anandgh/ajjLyd57/

您可以使用如下class而非ID类:

 $('.dateRangePicker')...

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

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