Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在待办事项列表中,通过jquery&x2B;选中复选框时,将记录保存在数据库中;ajax脚本_Jquery_Ajax - Fatal编程技术网

在待办事项列表中,通过jquery&x2B;选中复选框时,将记录保存在数据库中;ajax脚本

在待办事项列表中,通过jquery&x2B;选中复选框时,将记录保存在数据库中;ajax脚本,jquery,ajax,Jquery,Ajax,我想在数据库中记录在待办事项列表中完成的任务。为此,我需要通过jquery+aajx-send将选中/未选中的数据发送到我的BDD。你能帮我更正这个脚本吗?干杯 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function(

我想在数据库中记录在待办事项列表中完成的任务。为此,我需要通过jquery+aajx-send将选中/未选中的数据发送到我的BDD。你能帮我更正这个脚本吗?干杯

<script 
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">  
$(document).ready(function(){
$('input:checkbox').click(function() {
$('input:checkbox').is(':checked');
 $.ajax({
            type:'POST',
            url: 'send.php',
            data: 'id='+$(this).attr(("input:checkbox").checked()"),
            success: function(){ },
            error: function(){ alert('failed');}
    }); 
});
});
</script>

<html><body>
<input type="checkbox" class="custom-control-input" id="taskdone_1" >
<input type="checkbox" class="custom-control-input" id="taskdone_2" >
</body></html>

$(文档).ready(函数(){
$('input:checkbox')。单击(函数(){
$('input:checkbox')。是(':checkbox');
$.ajax({
类型:'POST',
url:'send.php',
数据:“id=”+$(this).attr((“输入:复选框”).checked()”,
成功:函数(){},
错误:函数(){alert('failed');}
}); 
});
});

1st:
。更改
而不是
。单击

2nd:传递数据,如
data:{id:id\u Here}

3rd:要获取id,请使用
.attr('id')


您必须了解代码中的一些要点:

首先,这一行:
$('input:checkbox')。是(':checked');
没有任何类似的含义。它返回
布尔值
,因此必须将其用于条件

if ($('input:checkbox').is(':checked')) {
    // action here
}
然后,您必须使用
事件更改为而不是
单击
事件

Last,将数据作为
对象传递,而不是直接传递。包含
'id='+$(this).attr((“输入:复选框”).checked()”
的行也没有任何意义

这是你的密码

$('input:checkbox').on('change',function() {
    if(this.checked){ // short hand of $(this).is(':checked')
      $.ajax({
        type:'POST',
        url: 'send.php',
        data: { id : $(this).attr('id') },
        dataType: 'json'
        success: function(response){ console.log(response); },
        error: function(){ alert('failed');}
     });
   }
});
谢谢你的回答。它可以工作,但是我必须在我的待办事项列表中添加一个未检查任务的条件。因此,完整代码是:

<script 
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
 </script>
<script type="text/javascript">  
$(document).ready(function(){
   $('input:checkbox').on('change',function() {
    var ThisIt = $(this),
    Data = { id : ThisIt.attr('id') };

    if(this.checked){   
      $.ajax({
        type:'POST',
        url: 'send1.php',
        data: Data,
        success: function(response){ console.log(response); },
        error: function(){ alert('failed');}
      });
    }
    else {  
      $.ajax({
        type:'POST',
        url: 'send2.php',
        data: Data,
        success: function(response){ console.log(response); },
        error: function(){ alert('failed');}
      });
    }   

    });
});
</script>

$(文档).ready(函数(){
$('input:checkbox')。在('change',function()上{
var ThisIt=$(this),
Data={id:ThisIt.attr('id')};
如果(这个.勾选){
$.ajax({
类型:'POST',
url:'send1.php',
数据:数据,
成功:函数(响应){console.log(响应);},
错误:函数(){alert('failed');}
});
}
否则{
$.ajax({
类型:'POST',
url:'send2.php',
数据:数据,
成功:函数(响应){console.log(响应);},
错误:函数(){alert('failed');}
});
}   
});
});
$('input:checkbox').on('change',function() {
    if(this.checked){ // short hand of $(this).is(':checked')
      $.ajax({
        type:'POST',
        url: 'send.php',
        data: { id : $(this).attr('id') },
        dataType: 'json'
        success: function(response){ console.log(response); },
        error: function(){ alert('failed');}
     });
   }
});
<script 
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
 </script>
<script type="text/javascript">  
$(document).ready(function(){
   $('input:checkbox').on('change',function() {
    var ThisIt = $(this),
    Data = { id : ThisIt.attr('id') };

    if(this.checked){   
      $.ajax({
        type:'POST',
        url: 'send1.php',
        data: Data,
        success: function(response){ console.log(response); },
        error: function(){ alert('failed');}
      });
    }
    else {  
      $.ajax({
        type:'POST',
        url: 'send2.php',
        data: Data,
        success: function(response){ console.log(response); },
        error: function(){ alert('failed');}
      });
    }   

    });
});
</script>