[英]Save check and uncheck values from checkboxes
我有这个表格:
<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!
使用JS获取复选框的值:
var checkboxValue = document.getElementById("myCheck").checked // return true or false
并为您的复选框提供ID。
如果是复选框,则只有选中该复选框,该值才会被发布。 因此,在将其保存到数据库时,需要使用以下代码检查是否在PHP中设置了复选框字段值。
$checkboxValue = isset($_REQUEST['earning_sound']) ? 1 : 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);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.