我在我的网站上有一个联系表格,在这个表格中我创建了一个带有span的复选框。要在数据库中插入复选框值,我使用隐藏输入,但无论是否选中复选框,该值都不会保存在数据库中
$('.checkbox').on('click', function(){
if($(this).find('.figure-container div').hasClass('clicked'))
{
$(this).find('.figure-container div').addClass('face').removeClass('entypo-check clicked').animate('slow');
}else{
$(this).find('.figure-container div').removeClass('face').addClass('entypo-check clicked').animate('slow');
}
});
<html>
<body>
<form action="" method="POST" >
<section>
<div class="wrapper-checkbox">
<div class="checkbox">
<div class="figure-container">
<div class="figure face"></div>
</div>
<div class="text">
<span name="check1">rent</span>
<input type="hidden" name="check1" value="rent">
</div>
</div>
</div>
</section>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src="js-checkbox/index.js"></script>
</form>
<?php
$check1 = !empty($_POST['check1']) ? $_POST['check1'] : '';
if ($stmt = mysqli_prepare($connect, "INSERT INTO $db_table VALUES (?)"))
{
mysqli_stmt_bind_param($stmt, "s",$check1);
?>
</body>
</html>
答案 0 :(得分:0)
您根本不更改输入字段
你的代码太复杂了,但需要这样的东西(未经过测试)
$('.checkbox').on('click', function() {
var value="";
if ($(this).find('.figure-container div').hasClass('clicked')) {
$(this).find('.figure-container div').addClass('face').removeClass('entypo-check clicked').animate('slow');
} else {
$(this).find('.figure-container div').removeClass('face').addClass('entypo-check clicked').animate('slow');
value="rent";
}
$(this).closest(".wrapper-checkbox").find("input[name=check]").val(value);
});
使用
<div class="wrapper-checkbox">
<div class="checkbox">
<div class="figure-container">
<div class="figure face"></div>
</div>
<div class="text">
<span name="check1">rent</span>
</div>
</div>
<input type="hidden" name="check" value="">
</div>