简体   繁体   English

使用ajax提交复选框数据

[英]checkbox data submit using ajax

<html>
<body>
<input type="checkbox" checked="checked">
</body>
</html>

I want to pass the value of the checkbox as 1 or 0 according to selection.我想根据选择将复选框的值传递为 1 或 0。 If it is checked then 1 is sent to a PHP file else 0 is sent.如果选中,则将 1 发送到 PHP 文件,否则发送 0。 The value is sent using a onClick event in the html, using AJAX I want to send the value.该值是使用 html 中的 onClick 事件发送的,使用 AJAX 我想发送该值。

I'm new to AJAX so unable to figure out a way to accomplish this task.我是 AJAX 的新手,所以无法找到完成这项任务的方法。

Thanks in advance.提前致谢。

$(document).ready(function(){
  $('body').delegate('#Yourbuttonid','click',function(){
        var chkval = 0
          if($('#yourcheckboxid').is(':checked')){
            chkval  = 1;
          }
   $.ajax({
       url: "Your_url",
       type: "POST",
       data:{'checkboxvalue':chkval},

       success:function(returndata){


       },error:function(errordata){


       }
     });

   });  

});

and in your Php file并在您的 PHP 文件中

You can get the checkbbox value as echo $_POST['checkboxvalue'];您可以获得复选框值作为echo $_POST['checkboxvalue'];

try尝试

$('element').on('click', function(){
$checkbox = $(':checkbox').prop('checked');
​$.post({ 
          url :'PageWhereYouWanToSend.php'​​​​​​​​​​​​​​​​​​​​​​​​​​,
          data : {​ 'checked' : $checkbox },
          success : function (resp){
                     alert(resp);
                   }
      });
​});​

References:参考:

.prop

.post

.on

var subcheck = 0;
$('input:checkbox').change(function(){
   if($(this).is(':checked')){
      subcheck = 1;
   } else {
      subcheck = 0;
   }


   $.post("your.php", { checkdata: subcheck},
   function(data) {
      //your code here
   });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM