简体   繁体   中英

Set startDate and endDate of Bootstrap datepicker dynamically based on selected value

I want to set startDate and endDate of Bootstrap datepicker dynamically based on value selected in option form.

So if I selected Boby in option then the datepicker will set startDate = 2016-10-04 and endDate = 2016-10-14.

The Array:

 <?php
    $data = array ( 
        [0] => Array ( [id] => 2 [name] => Anton [date_start] => 2016-10-04 [date_end] => 2016-10-14 ) 
        [1] => Array ( [id] => 4 [name] => Boby [date_start] => 2016-10-09 [date_end] => 2016-10-29 ) 
        [2] => Array ( [id] => 5 [name] => Ciara [date_start] => 2016-10-01 [date_end] => 2016-10-31 ) 
        [3] => Array ( [id] => 6 [name] => Don [date_start] => 2016-10-05 [date_end] => 2016-12-31 ) 
        [4] => Array ( [id] => 7 [name] => Ester [date_start] => 2016-10-01 [date_end] => 2016-12-31 ) 
        )
    ?>

Select

<select name="date" id="date">
<?php
foreach ($data as $r) {
echo "<option value='$r['id']'>$r[name]</option>";
}
?>
</select>

Input date

<label class="col-sm-3 control-label">Start</label>
<input id="date_start" name="date_start" type="text" class="form-control>
<label class="col-sm-3 control-label">End</label>
<input id="date_end" name="date_end" type="text" class="form-control">

Anyone has an idea to solve this problem?

Update ===============================================================

I've tried with jQuery but it didn't work. Here is my JS code

$(document).ready(function () {
        $('#date').change(function () {
            var start;
            var end;
            var selected = $(this).find("option:selected").val();

<?php
$js_array = json_encode($data);
echo "var data = " . $js_array . ";\n";
?>

            for (var i = 0, len = data.length; i < len; i++) {
                if (data[i]['id'] === selected) {
                    start = data[i]['date_start'];
                    end = data[i]['date_end'];
                }
            }

            $('input').datepicker({
                startDate: start,
                endDate: end,
                dateFormat: "yyyy-mm-dd"
            });
        });
    });

Just change the equal sign to be = =, and add val function to change the date with selector to like this:

$(document).ready(function () {
    $('#date').change(function () {
        var start;
        var end;
        var selected = $(this).find("option:selected").val();

        //your php code

        for (var i = 0, len = data.length; i < len; i++) {

            if (data[i]['id'] == selected) {
                start = data[i]['date_start'];
                end = data[i]['date_end'];
            }
        }

        $("#date_start").val(start);
        $("#date_end").val(end);
        $('input').datepicker({
            dateFormat: "yy-mm-dd"
        });
    });
});

modify your for each loop to ouput like below

<select name="date" id="date">
    <option value="{'startDate':'2016-10-04','endDate':'2016-10-14'}">Anton</option>
    <option value="{'startDate':'2016-10-09','endDate':'2016-10-29'}">Boby</option>
</select>

after that you can easily get the dates on select with javascript.

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