0

ちょっとした jQuery 初心者ですが、StackOverflow のおかげですぐに学習できます。

フォームからデータを取得し、jQuery を使用して (セッション Cookie ではなく) Cookie に書き出すコードを教えてください。ここから jQuery Cookie プラグインをロードしています。

https://github.com/carhartl/jquery-cookie

また、DOM の準備が整ったときにコードをロードする必要があるかどうかも知りたいと思います。DOM についてはまだ理解できません。

前もって感謝します。

以下の返信に感謝します。ただし、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 });
    });

私のクッキーが設定されていない理由はありますか? ブラウザは受け入れるように設定されており、Firebug でデバッグしています。

4

2 に答える 2

1

永続的な Cookie はセッション Cookie と同じですが、有効期限があるため保持されます。

これにより、テキスト入力の値が 365 日で期限切れになる Cookie に書き込まれます。

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

...

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

編集

更新された質問ごとの新しいコード:

    $(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 });
        });

    });
于 2012-11-02T20:17:04.200 に答える
0

タグ付けから、GETを使用してフォームを送信したいと考えています。ここで説明する方法を使用して、URL 文字列値を抽出できます。

次に、Cookie に値を設定するだけです。

$.cookie('mycookie', getParameterByName('varname'), {expires: 100});

次のように Cookie の値を確認できます。

console.log($.cookie('mycookie'));
于 2012-11-02T20:16:03.200 に答える