根据选定的值动态设置Bootstrap datepicker的startDate和endDate

时间:2016-10-13 06:34:58

标签: javascript php jquery twitter-bootstrap datepicker

我想根据选项表单中选择的值动态设置Bootstrap datepicker的startDate和endDate。

因此,如果我在选项中选择了Boby,那么datepicker将设置startDate = 2016-10-04和endDate = 2016-10-14。

数组:

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

输入日期

<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">

任何人都有想法解决这个问题吗?

更新 ======================================= ========================

我尝试过使用jQuery,但它没有用。这是我的JS代码

$(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"
            });
        });
    });

2 个答案:

答案 0 :(得分:1)

只需将等号更改为= =,并添加val函数以使用选择器更改日期,如下所示:

nPy4JJavaError: An error occurred while calling o172.save.
: org.apache.spark.SparkException: Job aborted due to stage failure: Task 3 in stage 38.0 failed 10 times, most recent failure: '
Lost task 3.9 in stage 38.0 (TID 1811, yp-spark-dal09-env5-0046): spray.http.IllegalUriException: The path of an URI without authority must not begin with "//"

答案 1 :(得分:0)

将每个循环修改为输出,如下所示

<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>

之后,您可以使用javascript轻松获取所选日期。