चेकबॉक्स मूल्यों के हठ

वोट
2

मैं कई चेकबॉक्स के साथ एक पृष्ठ है। मैं उनमें से कुछ की जाँच करें और, अगले पृष्ठ पर जाने जब मैं इस पृष्ठ पर वापस आने के लिए, निम्न चेक बॉक्स की जाँच की रहने के लिए के रूप में वे एक और पेज पर आने से पहले थे की जरूरत है। जावास्क्रिप्ट के साथ यह करने के लिए की जरूरत है। कोई सुराग??

20/07/2009 को 16:28
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


3 जवाब

वोट
1

आप पृष्ठ-अनुरोधों के बीच उन्हें लागू करने के लिए की आवश्यकता होगी। आप सत्र या कुकीज़ का उपयोग करते हैं कर सकते हैं। किस प्रकार का सर्वर आप पर काम कर रहा है, और सर्वर साइड भाषा के लिए किस प्रकार के साथ कर रहे हैं?

अतः पते के लेखन है / जावास्क्रिप्ट से कुकीज़ पढ़ने पर पिछला सवाल।

20/07/2009 को 16:30
का स्रोत उपयोगकर्ता

वोट
4

आप केवल जावास्क्रिप्ट और कोई सर्वर साइड भाषा तक सीमित कर रहे हैं मैं तुम्हें राज्य बनाए रखने के लिए / कुकीज़ लेखन को पढ़ने के लिए छोड़ दिया जाता है लगता है। के रूप में दूसरों को संदर्भित किया है, सर्वर साइड प्रौद्योगिकियों ज्यादा इस पर बेहतर है लेकिन अगर आप चाहिए रहे हैं:

जावास्क्रिप्ट कुकी नमूना कोड (संदर्भ: http://www.quirksmode.org/js/cookies.html ):

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}
20/07/2009 को 16:37
का स्रोत उपयोगकर्ता

वोट
0

मैं वहाँ सर्वर-साइड कोड के लिए किसी भी पहुंचे बिना यह करने के लिए किसी भी हद जटिल तरीका है, क्योंकि कम से कम आपको नहीं लगता कि जरूरत है अपने कोड को स्थापित करने और भी ताकि उन्हें जांच करने के लिए जैसे एचटीएमएल नियंत्रण की पहचान करने के लिए। मैं करता है कि क्या आप नीचे दिए गए चाहते उचित कोड दे रहा हूँ '।

महत्वपूर्ण लेख:

  1. कोड की आवश्यकता है कि प्रत्येक बॉक्स का एक विशिष्ट आईडी गुण दिया जाता है।
  2. जांच राज्य 'JS_PERSISTENCE_COOKIE' नामक एक कुकी में संग्रहीत किया जाता है। यह अलग-अलग स्थानों के एक जोड़े में यह हार्डकोड के रूप में मैंने किया है के बजाय एक चर में इस कुकी के नाम स्टोर करने के लिए बेहतर होगा। चर नाम संग्रहीत करना चाहिए किस तरह अपने अनुप्रयोग और आवश्यकताओं पर निर्भर करता है।
  3. बेहतर होगा के रूप में मैंने किया है एक वस्तु के अंदर के बजाय मुक्त कार्यों का एक समूह के रूप में कोड पैकेज होगा। बहरहाल, यह अपने प्रारंभिक सवाल के लिए प्रासंगिक नहीं है।
  4. कुछ चेक बॉक्स क्लिक करने के बाद, आप Ctrl + F5 दबाकर "इस पृष्ठ पर यह नेविगेट" अनुकरण कर सकते हैं। अकेले F5 पर्याप्त नहीं है।

यहाँ कोड और परीक्षण के लिए कुछ नमूना HTML है:

<body onload="restorePersistedCheckBoxes()">
    <input type="checkbox" id="check1" onclick="persistCheckBox(this)" />
    <input type="checkbox" id="check2" onclick="persistCheckBox(this)" />
    <input type="checkbox" id="check3" onclick="persistCheckBox(this)" />
    <input type="button" value="Check cookie" 
           onclick="alert(document.cookie)" />
    <input type="button" value="Clear cookie"
           onclick="clearPersistenceCookie()" />

    <script type="text/javascript">
        // This function reads the cookie and checks/unchecks all elements
        // that have been stored inside. It will NOT mess with checkboxes 
        // whose state has not yet been recorded at all.
        function restorePersistedCheckBoxes() {
            var aStatus = getPersistedCheckStatus();
            for(var i = 0; i < aStatus.length; i++) {
                var aPair = aStatus[i].split(':');
                var el = document.getElementById(aPair[0]);
                if(el) {
                    el.checked = aPair[1] == '1';
                }
            }
        }

        // This function takes as input an input type="checkbox" element and
        // stores its check state in the persistence cookie. It is smart
        // enough to add or replace the state as appropriate, and not affect
        // the stored state of other checkboxes.    
        function persistCheckBox(el) {
            var found = false;
            var currentStateFragment = el.id + ':' + (el.checked ? '1' : '0');
            var aStatus = getPersistedCheckStatus();
            for(var i = 0; i < aStatus.length; i++) {
                var aPair = aStatus[i].split(':');
                if(aPair[0] == el.id) {
                    // State for this checkbox was already present; replace it
                    aStatus[i] = currentStateFragment;
                    found = true;
                    break;
                }
            }
            if(!found) {
                // State for this checkbox wasn't present; add it
                aStatus.push(currentStateFragment);
            }
            // Now that the array has our info stored, persist it
            setPersistedCheckStatus(aStatus);
        }

        // This function simply returns the checkbox persistence status as
        // an array of strings. "Hides" the fact that the data is stored
        // in a cookie.
        function getPersistedCheckStatus() {
            var stored = getPersistenceCookie();
            return stored.split(',');
        }

        // This function stores an array of strings that represents the 
        // checkbox persistence status. "Hides" the fact that the data is stored
        // in a cookie.
        function setPersistedCheckStatus(aStatus) {
            setPersistenceCookie(aStatus.join(','));
        }

        // Retrieve the value of the persistence cookie.
        function getPersistenceCookie()
        {
          // cookies are separated by semicolons
          var aCookie = document.cookie.split('; ');
          for (var i=0; i < aCookie.length; i++)
          {
            // a name/value pair (a crumb) is separated by an equal sign
            var aCrumb = aCookie[i].split('=');
            if ('JS_PERSISTENCE_COOKIE' == aCrumb[0]) 
              return unescape(aCrumb[1]);
          }
          return ''; // cookie does not exist
        }

        // Sets the value of the persistence cookie.
        // Does not affect other cookies that may be present.
        function setPersistenceCookie(sValue) {
            document.cookie = 'JS_PERSISTENCE_COOKIE=' + escape(sValue);
        }

        // Removes the persistence cookie.
        function clearPersistenceCookie() {
            document.cookie = 'JS_PERSISTENCE_COOKIE=' +
                              ';expires=Fri, 31 Dec 1999 23:59:59 GMT;';
        }
    </script>

</body>
20/07/2009 को 17:26
का स्रोत उपयोगकर्ता

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more