简体   繁体   English

如何将Cookie数据值插入隐藏的表单字段?

[英]How do I insert a cookie data value into a hidden form field?

I would like to be able to take data stored in a cookie and insert it into a hidden form value. 我希望能够获取存储在cookie中的数据并将其插入到隐藏的表单值中。

I am storing the data in a cookie using the following: 我使用以下方法将数据存储在cookie中:

<script type="text/javascript">
    $(function () {
        $("#btnCookie").bind("click", function () {
            $.cookie("name", $("#txtName").val());

I am able to retrieve the value as plain text on the next page using: 我可以使用下一页在纯文本上检索值:

$.cookie("name")

I have a form on the 2nd page that I want this value to be a part of. 我在第二页上有一个表单,希望该值成为其中的一部分。 How can I insert it into the following: 如何将其插入以下内容:

<input type="hidden" name="cookie" />

I have tried this but it doesn't work: 我已经尝试过了,但是没有用:

<input type="hidden" name="cookie" value="$.cookie("name")" />

You can do it like this, 你可以这样

var cookie_val = $.cookie("name");
$('input[name=cookie]').val(cookie_val);

<input type="hidden" name="cookie" value="$.cookie('name')" />

双引号导致HTML中的错误。

Try this jquery cookie u can use in php. 试试这个可以在php中使用的jquery cookie。

value="<?php echo $_COOKIE['name']; ?>"
or use jquery
$('input[name=cookie]').val($.cookie("name"));

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

相关问题 我该如何做onclick并将值保存到隐藏表单的rails中? - How can I do onclick and save value to rails form hidden field? 如何在表单查找期间将隐藏的 SQL ID 字段值与其他数据一起传递? - How can I pass a hidden SQL ID field value alongside other data during a form lookup? 如何从Webhook中获取数据并将其输入到Weebly Web表单的隐藏字段中? - How do I take data from my webhook and enter it into a hidden field on a Weebly web form? 将值传递给隐藏的表单字段/解析数据 - Passing a value to a hidden form field / Parse data 如何在文本文件中打印隐藏字段的值? - How do I print the value of a hidden field in a text file? 根据选择表单将不同的值插入隐藏的输入字段 - Insert different value into hidden input field based on select form 如何使用JavaScript中的XMLHttpRequest更改“ Cookie”(在标头字段中)中一个cookie的值? - How do I change the value of one cookie in 'Cookie' (in the header field) with XMLHttpRequest in JavaScript? 如何在输入表单字段中插入当前日期作为值 - How can i insert current date as a value in an input form field 如何通过JSP代码将隐藏字段(Javascript)插入XML? - How do I insert hidden field (Javascript) into XML via JSP codes? 选择某个选项后,如何将值传递给隐藏的表单字段? - How do you pass a value to a hidden form field when a certain option is selected?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM