我需要保存我的頁面上覆選框的值以及狀態;所以我可以在提交時檢查複選框是否未選中,並使用comparestatus()函數的值來獲取更多邏輯。我已經試過這個,我得到的錯誤是變量periodCHK沒有定義。如何保存複選框值和狀態
<input type="checkbox" name="Periods" value="25301601" >
<input type="checkbox" name="Periods" value="25301602" checked>
<script type="text/javascript">
$j(document).ready(function() {
var periodCHK;
$j(":checkbox").each(function() {
periodCHK["$j(this.id).val()"]=($j(this).is(':checked'));
});
console.log(periodCHK);
function comparestatus() {
var periodstring;
if (!$j(this).is(':checked')) {
// if this one was checked before, grab it and make a string
if (window.periodCHK["$j(this.id).val()"])
{
periodstring+=["$j(this.id).val()"] + ",";
}
}
}
});
</script>
您必須將periodCHK定義爲Object,'var periodCHK = {}' –