Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.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/jquery/68.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/0/windows/16.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
Javascript 使用JQUERY获取单击复选框的值并将其传递给ajax_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 使用JQUERY获取单击复选框的值并将其传递给ajax

Javascript 使用JQUERY获取单击复选框的值并将其传递给ajax,javascript,jquery,ajax,Javascript,Jquery,Ajax,单击复选框时,我希望使用ajax发出post请求 我的复选框如下所示: <input name="foton" type="checkbox" onclick="processForm(this)" value='.$f->group_id.'>Închis</input> function processForm(e) { jQuery.ajax( { type: 'POST', url: '/ajax/checkbox.php',

单击复选框时,我希望使用ajax发出post请求

我的复选框如下所示:

<input name="foton" type="checkbox" onclick="processForm(this)" 
value='.$f->group_id.'>Închis</input>
function processForm(e) { 
    jQuery.ajax( {
    type: 'POST',
    url: '/ajax/checkbox.php',
    data: { foton : jQuery(e).val()},
    success: function(data) {
        jQuery('#message').html(data);
    }
    });
}

我做错了什么?

这是当前语法。因为您是从onclick事件传递对象本身,所以不需要将其转换为jquery对象来获取其值。只需
此.value
即可给出基础值

 data: { foton : e.value},

函数processForm(e){
$.ajax({
键入:“POST”,
url:“/ajax/checkbox.php”,
数据:{foton:e.value},
成功:功能(数据){
jQuery('#message').html(数据);
}
});
}

我相信你应该能够利用e.value来传递你的复选框值。但是,这很难排除故障,因为我不知道您的问题是什么。

另一个答案是将其全部移动到JQuery并传递JQuery元素。工作示例:

或在更改或单击时切换到:

<script>
function processForm(e) { 
    $.ajax( {
    type: 'POST',
    url: '/ajax/checkbox.php',
    data: { foton : e.value },
    success: function(data) {
        jQuery('#message').html(data);
    }
    });
}</script>
function processForm(e) { 
    $.ajax({
        type: 'POST',
        url: '/ajax/checkbox.php',
        data: {
            foton: e.val()
        },
        success: function(data) {
            $('#message').html(data);
        }
    });
}

$(document).ready(function(){
  $("input[name='foton']").on("click", function() {
    processForm($(this));
  });
});