我的模态不是从cookie更新新数据

时间:2017-12-08 10:44:09

标签: javascript php jquery html bootstrap-modal

我在使用cookie中的新数据更新模式时遇到问题。当我在表格中选择新项目并将其推送到cookie时。然后,当我按下按钮时,它不会更新我的模态视图。

有人可以弄清楚它为什么不起作用。

<script>
var selVal = new Array();

$("tbody tr").click(function () {
    if ($(this).hasClass('selected')) {
        $(this).removeClass('selected');
        var pId = $(this).find('td:first').data("id"); // Get id num from selected

        //Remove item from array
        var i = selVal.findIndex(x => x.pId === pId);

        if (i !== -1) {
            selVal.splice(i, 1);
        }
    } else {
        $(this).addClass("selected");
        var pId = $(this).find('td:first').data("id"); // Get id num from selected
        var pNum = $(this).find('td:first').html(); //Get pnum from selected

        var newObjArr = {pId, pNum};

        selVal.push(newObjArr);
    }
    createCookie(selVal);
});

function createCookie(selVal) {

    var json_str = JSON.stringify(selVal); // JSON Serialize

    // Define/Set cookie
    Cookies.set("Cookie_<?php echo $id?>", json_str, {expires: 1});
}

    <!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="ModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="ModalLabel">XXXXXXXXXXXXXXXX</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>

            <div class="modal-body">
                <button type="button" id="refdata_btn" class="btn btn-info">Refresh</button> 
                <p><label>Airline: </label><input type="text" name='xxxxxx'></p>
                <p><label>Date: </label> <input type='date'></p>

                <?php
                $cookie_name = 'Cookie_' . $id;

                if (isset($_COOKIE[$cookie_name])) {

                    $decjson = json_decode($_COOKIE[$cookie_name], true);

                    foreach ($decjson as $d) {
                        echo '<p>' . $d['pNum'] . '</p>';
                    }
                } else {
                    echo 'Cookie not exist';
                }
                ?>
            </div>

            <div class="modal-footer">
                <button type="button" id='btn_later' class="btn btn-secondary" data-dismiss="modal">Later</button>
                <button type="button" class="btn btn-primary">Send</button>
            </div>
        </div>
    </div>
</div>

当我看到这一点时,它并没有给我特别的逻辑,为什么它不会重置或覆盖以前的数据。

谢谢, ZS

0 个答案:

没有答案