Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从表单获取数据并使用jQuery写入cookie_Jquery_Cookies_Get - Fatal编程技术网

从表单获取数据并使用jQuery写入cookie

从表单获取数据并使用jQuery写入cookie,jquery,cookies,get,Jquery,Cookies,Get,有点像jquerynoob,但由于StackOverflow,学习速度很快 有谁能给我一些代码,这些代码可以从表单中获取数据,并使用jQuery将数据写入cookie(而不是会话cookie)。我正在从这里加载jQuery cookie插件: 我还想知道,当DOM准备就绪时,是否必须加载我的代码,就像前面用php加载cookies时一样。我还没来得及弄明白 提前谢谢。 好的,这是当前的代码,谢谢下面的回复。虽然我无法使cookie正常工作,但未注释时警报工作正常: <

有点像jquerynoob,但由于StackOverflow,学习速度很快

有谁能给我一些代码,这些代码可以从表单中获取数据,并使用jQuery将数据写入cookie(而不是会话cookie)。我正在从这里加载jQuery cookie插件:

我还想知道,当DOM准备就绪时,是否必须加载我的代码,就像前面用php加载cookies时一样。我还没来得及弄明白

提前谢谢。 好的,这是当前的代码,谢谢下面的回复。虽然我无法使cookie正常工作,但未注释时警报工作正常:

           <form id="myform">
              <fieldset>
                <h3>Search</h3>
                <p>
                  <label>Your search *</label>
                  <input type="text" id="mysearch" pattern="[a-zA-Z ]{5,}" maxlength="30" />
                </p>
                <fieldset>
                    <input type="range" id="range" min="1" max="30" value="10"  />
                    <input type="range" id="range2" min="30" max="9000" value="2000"  />
                 </fieldset>

                <button id="subbtn" type="submit">Submit form</button>
                <button type="reset">Reset</button>
              </fieldset>
            </form>


        $(function() {
        // initialize tooltip
        $(".imgwrap").tooltip({ 
        //$(".tooltip").tooltip({ effect: 'slide'});
           // tweak the position
           offset: [10, 2],

           // use the "slide" effect
           effect: 'slide'

        // add dynamic plugin with optional configuration for bottom edge
        }).dynamic({ bottom: { direction: 'down', bounce: true } });
        });

        //this one loads the form validator
        $("#myform").validator();

        //this one for the slider
        $(":range").rangeinput();

    $("#subbtn").on("click", function () {
    // alert("Cookie!");
        $.cookie('Searchitem', $("#mysearch").val(), { expires: 365 });
    });

搜寻

你的搜索*

提交表格 重置 $(函数(){ //初始化工具提示 $(“.imgwrap”)。工具提示({ //$(“.tooltip”).tooltip({effect:'slide'}); //调整位置 偏移量:[10,2], //使用“幻灯片”效果 效果:“幻灯片” //添加带有可选配置的动态插件,用于底部边缘 }).dynamic({bottom:{direction:'down',bounce:true}}); }); //这一个加载表单验证程序 $(“#myform”).validator(); //这个是滑块的 $(“:range”).rangeinput(); $(“#subbtn”)。在(“单击”,函数(){ //警报(“Cookie!”); $.cookie('Searchitem',$(“#mysearch”).val(),{expires:365}); });

你知道为什么我的饼干没有凝固吗?浏览器已设置为接受,我正在使用Firebug进行调试。

我假设您希望使用GET提交表单。您可以使用描述的方法提取URL字符串值

然后,只需将值设置为cookie:

$.cookie('mycookie', getParameterByName('varname'), {expires: 100});
您可以像这样检查cookie的值:

console.log($.cookie('mycookie'));

持久cookie与会话cookie相同,只是它有一个过期日期,因此会挂起

这将把文本输入的值写入365天后过期的cookie:

    <input id="txt" type="text" value="foo" />
    <input id="btn" type="button" value="write cookie" />
编辑

每个更新问题的新代码:

    $(function () {

        // initialize tooltip
        $(".imgwrap").tooltip({
            //$(".tooltip").tooltip({ effect: 'slide'});
            // tweak the position
            offset: [10, 2],

            // use the "slide" effect
            effect: 'slide'

            // add dynamic plugin with optional configuration for bottom edge
        }).dynamic({ bottom: { direction: 'down', bounce: true} });

        //this one loads the form validator
        $("#myform").validator();

        //this one for the slider
        $(":range").rangeinput();

        $("#subbtn").on("click", function () {
            // alert("Cookie!");
            $.cookie('Searchitem', $("#mysearch").val(), { expires: 365 });
        });

    });

使用你的代码Pete并添加到原始帖子中。仍然无法设置cookie,有什么想法吗?请确保所有代码都包装在$(document).ready()中。这让你的例子对我有用。查看我的最新答案。我会试试这个。谢谢。我认为使用$(function(){与使用$(document).ready()是一样的,那么我是否得到了一个错误的括号,或者这里没有得到什么东西?是的,它们是等效的。更新了我的答案以使用$(function(){..})。我认为您问题中发布的代码中的问题是,您在验证程序行之前关闭了封装函数,因此验证程序、rangeinput和click处理程序在DOM准备就绪之前运行,因此无法工作。感谢Mark,我们将查看您链接到的方法。
    $(function () {

        // initialize tooltip
        $(".imgwrap").tooltip({
            //$(".tooltip").tooltip({ effect: 'slide'});
            // tweak the position
            offset: [10, 2],

            // use the "slide" effect
            effect: 'slide'

            // add dynamic plugin with optional configuration for bottom edge
        }).dynamic({ bottom: { direction: 'down', bounce: true} });

        //this one loads the form validator
        $("#myform").validator();

        //this one for the slider
        $(":range").rangeinput();

        $("#subbtn").on("click", function () {
            // alert("Cookie!");
            $.cookie('Searchitem', $("#mysearch").val(), { expires: 365 });
        });

    });