如何在页面加载时存储复选框的值和状态
How to store value and status of checkbox at pageload
我希望在页面加载时存储复选框的值及其状态。当我提交表格时,我想检查前面的复选框"已选中"是否为"未选中";如果是这样,我需要将值保存在一个字符串中,以便在查询中用作列表。我尝试了以下内容,但我的语法有问题。请帮忙。
<input type="checkbox" name="Periods" value="25301601" />
<input type="checkbox" name="Periods" value="25301602" checked />
<input type="button" id="buttonStore" value="Store">
<input type="button" id="buttonCompare" value="Compare">
$(document).ready(function() {
$("#buttonStore").click(function () {
// save status and value of all the checkboxes
var periodCHK= [];
$(":checkbox").each(function (){
periodCHK["$(this).val()"]=($(this).is(':checked'));//does not store
alert($(this).is(':checked'));
alert($(this).val());
});
alert(periodCHK);//nothing
});
$("#buttonCompare").click(function () {
var periodstring;
$(":checkbox").each(function () {
if (!$(this).is(':checked'))
//if $(":checkbox").not(':checked')
{
//if this one was checked before, grab it and make a string
if (periodCHK["$(this).val()"])
{
periodstring+=["$(this).val()"] + ",";
}
}
alert(periodstring); // does not loop
});
});
});
在以下语句中使用不带引号的$(this).val()
。如果与引号一起使用,它将被视为字符串。
periodCHK[$(this).val()]
相关文章:
- 基于复选框状态的条件数学
- 导航到另一个页面后,复选框状态会更改
- 如何根据各种复选框状态执行操作
- 检查复选框状态-Javascript
- JavaScript-使用sessionStorage保存文档之间共享的复选框状态
- React.js可以't更改复选框状态
- 如何检查更改事件的复选框状态
- 基于各种用户行为管理复选框状态
- 根据 ajax 响应中的值设置复选框状态
- 从 django 读取复选框状态
- 分析获取查询字符串并使用值更改复选框状态
- 是否可以在不导致单击事件的情况下更改复选框状态
- 如何在 javascript 的信息窗口中使用 cookie 保存复选框状态
- 复选框提醒脚本在动态加载测试时不记得复选框状态.php其中包含复选框
- 保持选中复选框状态,直到用户取消选中
- 复选框状态(如果未选中)
- 复选框状态和其他属性
- 根据 AngularJS 中的多个条件更改复选框状态
- 自动将复选框状态/值保存到数据库
- 在复选框状态ExtJS 4.1中手动检测与编程更改