如何在表单提交后添加cookie来记住Jquery状态?

问题描述 投票:0回答:1

我想在提交后添加cookie以隐藏表单15分钟。我尝试了一切,但什么也没做。我从来没用过饼干!我很感谢你的帮助。它适用于“localStorage”但我无法设置过期时间。我的问题是,我在哪里放饼干?

<script type="text/javascript">

 $(document).ready(function (){

$('form#forma1').submit(function(e) {
    e.preventDefault();// will stop the form being submited...
    $(this).hide(1000);
    $('h3').show(1000);


   $.ajax({
                type: "POST",
                url: "povezii.php",
                data: $(this).serialize(),
                success: function(data) {
                    $('#chatbox').show();
                    $("#chatbox").append(data+"<br/>");//instead this line here you can call some function to read database values and display

                },

            });
    return false;


});

 });
    </script>

形成:

<div class="container">
<form id="forma1" class="forma1">

    <div class="col-md-6"> 

    <label name="odkoga"> From who: </label>
    <input type="text" name="odkoga" class="form-control"/> 

    <label name="zakoga"> For whom </label>
    <input type="text" name="zakoga" class="form-control"/>

    <label name="pjesmica"> Music wish: </label>
    <input type="text" name="pjesmica" class="form-control"/>
    <br>
    <input type="submit" class="btn btn-success btn-block" value="Naruci"/>

    </div>

</form>
</div>
jquery cookies
1个回答
0
投票
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.js"></script>

<script type="text/javascript">

  $(document).ready(function (){

  if (!!$.cookie('foo')) {
    alert('Iducu zelju mozete naruciti za 15-ak minuta od zadnje zelje, do tada uzivajte uz talase Radio Tavije! Vas Radio Tavija team.');
    console.log("aa");
    $('#forma1').hide(500);
} else {
 // no cookie
$('form#forma1').submit(function(e) {
        e.preventDefault();// will stop the form being submited...
        $(this).hide(1000);
        $('h3').show(1000);

    $.ajax({
                    type: "POST",
                    url: "povezii.php",
                    data: $(this).serialize(),
                    success: function(data) {
                        $('#chatbox').show();
                        $("#chatbox").append(data+"<br/>");//instead this line here you can call some function to read database values and display
                        var expire = new Date();
                        //set expiry to current time plus 15 minutes in milliseconds
                        expire.setTime(expire.getTime() + (15 * 60 * 1000)); 
                        $.cookie('foo', 'test', {expires: expire});

                    },
                });
                return false;
         });
    }
 });
</script>
© www.soinside.com 2019 - 2024. All rights reserved.