2014-03-02 66 views
0

我有一个下拉框,当从下拉列表中选择数据时,我也有每个td上面的复选框,这个复选框用于隐藏这个执行的列Java脚本,如果用户选中该复选框,他选择在下拉框中的另一个值,则选择复选框将不显示,如何保持复选框刷新页面后检查

下面

是隐藏的列时复选框选中

代码我想如果用户选中复选框,并且他在下拉框中选择了另一个值,那么所选的复选框将不会显示任何人可以如何操作

<input type='checkbox' style='margin:-19px 0 0 732px; border: 1px solid grey;' name='9xx'/>9xx 
<input type='checkbox' style='margin:-19px 0 0 36px; border: 1px solid grey;' name='6xx'/>6xx 
<input type='checkbox' style='margin:-19px 0 0 30px; border: 1px solid grey;' name='12xx'/>12xx 
<input type='checkbox' style='margin:-19px 0 0 21px; border: 1px solid grey;' name='14xx'/>14xx 
<input type='checkbox' style='margin:-19px 0 0 26px; border: 1px solid grey;' name='10xx'/>10xx 
<input type='checkbox' style='margin:-19px 0 0 31px; border: 1px solid grey;' name='8xx'/>8xx 
<input type='checkbox' style='margin:-19px 0 0 31px; border: 1px solid grey;' name='11xx'/>11xx 

<script> 
$("input:checkbox:not(:checked)").each(function() { 
    var column = "table ." + $(this).attr("name"); 
    $(column).show(); 
}); 

$("input:checkbox").click(function(){ 
    var column = "table ." + $(this).attr("name"); 
    $(column).toggle(); 
}); 

</script> 
+0

将在cookie中的复选框的状态,并用它来初始化复选框当页面加载时。 – Barmar

+1

可能重复[保持复选框页面刷新后检查](http://stackoverflow.com/questions/9472590/keep-checkboxes-checked-after-page-refresh) – Barmar

+1

@Barmar为什么在地球上人们仍然想使用cookie为了这些事情。我想我们应该开始使用'localStorage'。 –

回答

8

使用localStorage它。

这里是JSFiddle它的例子。 Link

背后代码:

HTML代码:

<input type="checkbox"> 

JS代码:

$(function(){ 
    var test = localStorage.input === 'true'? true: false; 
    $('input').prop('checked', test || false); 
}); 

$('input').on('change', function() { 
    localStorage.input = $(this).is(':checked'); 
    console.log($(this).is(':checked')); 
}); 
+2

该解决方案将检查该页面上的所有“复选框”,即使您选择了一个复选框。 –

+1

是的,我知道。但是任何熟悉jQuery的人都可以为单个复选框编辑此代码:)这并不困难。 –

+0

要阅读本地存储http://www.w3schools.com/html/html5_webstorage.asp – Matheus208

相关问题