我有这样的表格:
<form name="settings" action="../ajax/account/account_ajax.php?token=<?php echo $token; ?>"
class="form-horizontal" method="post">
<input type="hidden" name="set_settings" value="1">
<div class="row">
<div class="col-md-5">
<label class="checkbox-custom">
<input name="earning_sound"
<?= ($settings->earning_sound) ? 'checked="checked"' : ''; }?>
value="1" type="checkbox">
<i class="fa fa-fw fa-square-o checked"></i>
Earning Notification Sound
</label>
<button type="submit" class="btn btn-primary ">
<i class="fa fa-edit"></i> Save
</button>
</div>
</div>
</form>
当我检查时如何以值1保存,以及当我取消选中时如何以值0保存!
答案 0 :(得分:0)
使用JS获取复选框的值:
var checkboxValue = document.getElementById("myCheck").checked // return true or false
并为您的复选框提供ID。
答案 1 :(得分:0)
如果选中复选框,则仅在选中复选框时才会发布值。 因此,您需要检查是否在PHP中使用以下代码设置了复选框字段值,同时将其保存到数据库。
$checkboxValue = isset($_REQUEST['earning_sound']) ? 1 : 0;
答案 2 :(得分:0)
<form name="settings" action="../ajax/account/account_ajax.php?token=<?php echo $token; ?>" class="form-horizontal" method="post">
<input type="hidden" name="set_settings" value="1">
<div class="row">
<div class="col-md-5">
<label class="checkbox-custom">
<input name="earning_sound" class="earning_sound" type="checkbox" value="<?php echo ($settings->earning_sound == 1)? '1' : '0' ?>" <?php echo ($settings->earning_sound == 1)? 'checked' : '' ?> />
<i class="fa fa-fw fa-square-o checked"></i>
Earning Notification Sound
</label>
<button type="submit" class="btn btn-primary "><span class="fa fa-edit"></span> Save</button>
</div>
</div>
</form>
//这将帮助您在选中时将值保存为1,或在未选中时将值保存为0
//将以下代码添加到您的js文件
$(".earning_sound").click(function() {
$(this).val(this.checked ? 1 : 0);
});