Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.8k views
in Technique[技术] by (71.8m points)

php - javascript keep checkbox checked after refresh page

hi i am javascript newbie.

I have checkbox that slideDown other input , i want to keep that checkbox checked and the other input showed after refreshing the page if the input checkbox checked

javascript:

function validateSitec(){
if (document.getElementById('sitecheck').checked){
$('#sitecheck').prop('checked', true);
$('#closedmsg').slideDown();
}else{
$('#closedmsg').slideUp();
$("#sitecheck").removeProp("checked").checkboxradio("refresh");
}
}

this my inputs:

<label for="sitecheck">
<span style="font-weight:bold;">close site+ msg:</span>
</label>
<input name="" type="checkbox" id="sitecheck" onclick="validateSitec()" /><span style="font-weight:bold;">click to activate msg</span><br>
<input type="text" name="closedmsg" id="closedmsg" style="width:440px;height:120px;display:none;" value="enter closed msg.."/>

i want if checked stay checked.. and wont change after refreshing the page , then when unchecked so back to normal and be unchecked when refreshing the page..

should i use php for making it not change after checking with javascript?

Edited:

Thanks to all for helping credit goes to : leiyonglin .

The working code for anyone who like to use it:

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

then use this codes working awesome :

JavaScript:

<script type="text/javascript">

function validateSitec(){
if (document.getElementById('sitecheck').checked){
    $('#sitecheck').prop('checked', true);
    $('#closedmsg').slideDown();
    $.cookie("cookieChecked", "#sitecheck");
}else{
    $('#closedmsg').slideUp();
    $("#sitecheck").removeProp("checked");
    $.cookie("cookieChecked","");
}
}


  $(function(){
      var cookieChecked = $.cookie("cookieChecked");
      if(cookieChecked){
          $('#sitecheck').prop('checked', true);
          $('#closedmsg').show();
      }else{
          $("#sitecheck").removeProp("checked");
          $('#closedmsg').hide();
      }
 })
    </script>

html inputs:

<label for="sitecheck">
<span style="font-weight:bold;">close site temp:</span>
</label>
<input name="" type="checkbox" id="sitecheck" onclick="validateSitec()" /><span style="font-weight:bold;">close site and add message</span><br>
<input type="text" name="closedmsg" id="closedmsg" style="width:440px;height:120px;display:none;" value="<?php echo $data['csitemsg']; ?>" />

This working perfect thx again all.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use cookie to maintain your data on page refresh by using cookie. I notice you are using JQuery in your code. Here is a JQuery-Cookie plugin which will let you use cookie to manage your data across page refresh.

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

Go to Usage section and you will find how to create, read, delete and set expire easily form the plugin.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...