2015-07-28 105 views

回答

0

您必須在服務器端執行此操作。無論是與PHP,ASP.NET或任何你使用。

這是唯一的方法。

,如果您的網站只是客戶端,那麼您將不得不實施它,而不需要任何頁面刷新。

2

您需要爲此使用cookie ......只要用戶點擊複選框,將其狀態存儲到cookie中,並只需在頁面加載時獲取cookie值,以便像在之前的選擇中一樣預先設置複選框。

HTML

<div> 
    <label for="option1">Option 1</label> 
    <input type="checkbox" id="option1"> 
</div> 
<div> 
    <label for="option2">Option 2</label> 
    <input type="checkbox" id="option2"> 
</div> 
<div> 
    <label for="option3">Option 3</label> 
    <input type="checkbox" id="option3"> 
</div> 
<button>Check all</button> 

抓取複選框值,使一個cookie出他們的

var checkboxValues = {}; 
$(":checkbox").each(function(){ 
    checkboxValues[this.id] = this.checked; 
}); 
$.cookie('checkboxValues', checkboxValues, { expires: 7, path: '/' }) 

功能來讀取cookie來預填充負載

function repopulateCheckboxes(){ 
    var checkboxValues = $.cookie('checkboxValues'); 
    if(checkboxValues){ 
    Object.keys(checkboxValues).forEach(function(element) { 
     var checked = checkboxValues[element]; 
     $("#" + element).prop('checked', checked); 
    }); 
    } 
} 

Working Fiddle

0

在這裏,我如何做到這一點:

<html> 

<input type="checkbox" <?php echo file_get_contents(".cktest"); ?> onclick="getFileFromServer('write_ckfile.php?target=.cktest&value='+this.checked, function(text){ show_write(text)});"> cktest with php<br/> 


<!-- with ajax read the checked attribut from file --> 
<input id="withajax" type="checkbox" onclick="getFileFromServer('write_ckfile.php?target=.cktest&value='+this.checked, function(text){ show_write(text)});"> cktest with ajax<br/> 


<script> 
function getFileFromServer(url, doneCallback) { 
    var xhr; 

    xhr = new XMLHttpRequest(); 
    xhr.onreadystatechange = handleStateChange; 
    xhr.open("GET", url, true); 
    xhr.send(); 

    function handleStateChange() { 
     if (xhr.readyState === 4) { 
      doneCallback(xhr.status == 200 ? xhr.responseText : null); 
     } 
    } 
} 

function show_write(text) { 
    //alert(text); 
} 


// 
getFileFromServer(".cktest", function(x) { document.getElementById("withajax").checked=x;}); 

</script> 

</html> 

和文件write_ckfile.php:

<?php 
$strtarget=$_GET['target']; 


$status=$_GET['value']=="true"?"checked":""; 
file_put_contents($strtarget, $status); 



?> 

我希望這有助於。 MiKL〜

+1

請添加說明,而不僅僅是代碼 –

相關問題