我有一个复选框,并且想要将复选框的值提交到数据库。我希望没有提交按钮,
<label>OFF<input type="checkbox" checked><span class="lever"></span>ON</label>
答案 0 :(得分:0)
您可以尝试这样
$(document).ready(function(){
$('#aksi').change(function() {
var returnVal = confirm("Are you sure?");
if(returnVal){
postToServer($(this).prop("checked"))
}else{
$(this).prop("checked", !$(this).is(":checked"));
}
});
});
function postToServer(state){
let value = (state) ? 1 : 0;
alert('Posted Value: ' + value);
$.ajax({
type: "POST",
url: "http://your-server.com/update/aski",
data: {"aski":value},
success: function(response){
//handle response
}
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>OFF<input type="checkbox" id="aksi" checked><span class="lever"></span>ON</label>