我目前有以下代码(它是css和html的混合)用于切换。最终,用户可以切换数据库中的值以从0更改为1.但是,如果没有提交按钮,我不知道如何执行此操作。
CSS:
.onoffswitch {
position: relative; width: 45px;
-webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
}
.onoffswitch-checkbox {
display: none;
}
.onoffswitch-label {
display: block; overflow: hidden; cursor: pointer;
border: 2px solid #E3E3E3; border-radius: 10px;
}
.onoffswitch-inner {
width: 200%; margin-left: -100%;
-moz-transition: margin 0.3s ease-in 0s; -webkit-transition: margin 0.3s ease-in 0s;
-o-transition: margin 0.3s ease-in 0s; transition: margin 0.3s ease-in 0s;
}
.onoffswitch-inner:before, .onoffswitch-inner:after {
float: left; width: 50%; height: 15px; padding: 0; line-height: 15px;
font-size: 7px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold;
-moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box;
}
.onoffswitch-inner:before {
content: "ON";
padding-left: 5px;
background-color: #5B969C; color: #EEEEEE;
}
.onoffswitch-inner:after {
content: "OFF";
padding-right: 5px;
background-color: #EEEEEE; color: #5B969C;
text-align: right;
}
.onoffswitch-switch {
width: 15px; margin: 0px;
background: #FFFFFF;
border: 2px solid #E3E3E3; border-radius: 10px;
position: absolute; top: 0; bottom: 0; right: 26px;
-moz-transition: all 0.3s ease-in 0s; -webkit-transition: all 0.3s ease-in 0s;
-o-transition: all 0.3s ease-in 0s; transition: all 0.3s ease-in 0s;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
margin-left: 0;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
right: 0px;
}
HTML:
<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="onoffswitch6">
<label class="onoffswitch-label" for="onoffswitch6">
<div class="onoffswitch-inner"></div>
<div class="onoffswitch-switch"></div>
</label>
</div>
编辑:在答案之后我将此脚本添加到页面和PHP文件中。但是,它仅在数据库中更改为1,但不会将其更改回0。
脚本:
$(document).ready(function(){
$('#onoffswitch6').change(function() {
var checked = $(this).is(':checked'); //true or false?
$.ajax({
type: "post",
url: "encrypt.php", //PHP or whichever other language script that updates your database
data: { onOrOff : checked } //access this value in your script via $_POST['onOrOff'], if it's a PHP script
});
});
});
PHP文件:
<?php
//Start session
session_start();
//Include database connection details
require_once('connection.php');
if(isset($_POST['onOrOff']))
{
if ($_POST['onOrOff'] == false) {
global $onOrOff;
$onOrOff = 0;
} else if ($_POST['onOrOff'] == true) {
global $onOrOff;
$onOrOff = 1;
};
$qry = "UPDATE member SET value='$onOrOff' WHERE id='$_SESSION[SESS_MEMBER_ID]'";
$result = mysql_query($qry) or die("An error occurred ".mysql_error());
}
?>
答案 0 :(得分:1)
AJAX就是您所需要的。由于我没有太多信息,我会做一个小例子:
$('.onoffswitch-checkbox').change(function() {
var checked = $(this).is(':checked'); //true or false?
$.ajax({
type: "post",
url: "myscript.php", //PHP or whichever other language script that updates your database
data: { onOrOff : checked } //access this value in your script via $_POST['onOrOff'], if it's a PHP script
});
});