简体   繁体   English

如何使用jQuery Cookie设置表单的输入值

[英]How to set a input value of a form with jQuery Cookie

Hi I am learning jQuery and right now I am trying to use jQuery Cookie to set a form input value. 嗨,我正在学习jQuery,现在我正在尝试使用jQuery Cookie来设置表单输入值。

I have been able to set the cookie from a form using this: 我已经可以使用以下方法从表单中设置Cookie:

<script>
  $(document).ready(function () {
    $("#saveForm").click(function() {
      $.cookie('myCookie', $("#element_1").val(), { expires: 365 });
    });
  });
</script>

I was able to verify the cookie using an alert like this: 我可以使用如下警报来验证Cookie:

<script>
  alert( $.cookie("myCookie") );
</script>

But after searching numerous posts on here, I was not able to find the same question and I have looked at numerous tutorials and can't find the code to set the value of a input field in a form. 但是,在这里搜索了许多帖子之后,我找不到相同的问题,并且看了许多教程,也找不到用于设置表单中输入字段值的代码。 Thanks in Advance!! 提前致谢!!

in the dom ready handler set the value using .val() 在dom ready处理程序中使用.val()设置值

$(document).ready(function () {
    $("#saveForm").click(function () {
        $.cookie('myCookie', $el1.val(), {
            expires: 365
        });
    });

    //set the value of the cookie to the element element_1
    var $el1 = $("#element_1").val($.cookie("myCookie"))
});

You could do something as simple as: 您可以做一些简单的事情:

$(document).ready(function () {
   $("#saveForm").click(function() {
      $.cookie('myCookie', $("#element_1").val(), { expires: 365 });
   });
   $("#element_1").val($.cookie('myCookie'));
});

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

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