2015-07-28 84 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

请添加说明,而不仅仅是代码 –

相关问题