html - 如何在没有laravel提交按钮的情况下插入值复选框

标签 html mysql laravel

我有一个复选框,想将复选框的值提交给数据库。我宁愿没有提交按钮,

<label>OFF<input type="checkbox" checked><span class="lever"></span>ON</label>

enter image description here

最佳答案

你可以这样试试

$(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>

关于html - 如何在没有laravel提交按钮的情况下插入值复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50979494/

相关文章:

html - 删除 Excel HTML 嵌入中的网格线

jquery - 查找 div 元素并使用 jquery 替换 value-innerhtml

mysql - 在非主键列上为 Autoiuncrement 列指定默认起始值​​?

mysql - 如何在简单的书籍表中将多个作者记录到数据库中?

php - 根据子类在父类中包含类

php - 选择另一列的不同范围的数据计数和总和

javascript - 使用 document.execCommand ('insertHTML' 在 contentEditable 中插入 span,

python - 未找到 Django Python 页面 (404)

mysql - 从 codeigniter 调用存储过程不起作用

mysql - 如何仅将没有加入记录的模型添加到集合中 - Eloquent