Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.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/70.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 ajax提交多个变量_Javascript_Jquery - Fatal编程技术网

Javascript ajax提交多个变量

Javascript ajax提交多个变量,javascript,jquery,Javascript,Jquery,我刚开始学习jquery和ajax,我想有一个简单的问题: 我的html页面上有一个按钮,如下所示: <button type="button" id="i" id2="id2" name="name" class="button_red">-</button> 我只想用ajax方法将按钮中的所有变量(如id、id2、name、class)发送到update_data.php文件中。我如何以最好的方式做到这一点?那么如何使用then.serialize() 我的想法是:

我刚开始学习jquery和ajax,我想有一个简单的问题:

我的html页面上有一个按钮,如下所示:

<button type="button" id="i" id2="id2" name="name" class="button_red">-</button>
我只想用ajax方法将按钮中的所有变量(如id、id2、name、class)发送到update_data.php文件中。我如何以最好的方式做到这一点?那么如何使用then.serialize()

我的想法是:

var id = $(this).attr('id');
var id2 = $(this).attr('id2');
var name = $(this).attr('name');
var array = {id, id2, name};
//
//
//
data: array, array.serialize(),
succcess: ...

非常感谢

jQuery AJAX文档:
你也可以寄这个

1) 作为
对象

var DTO = {
  id: $(this).attr('id'),
  id2: $(this).attr('id2'),
  name: $(this).attr('name')
};
2) 作为
数组

var DTO = [
    $(this).attr('id'),  
  $(this).attr('id2'),
  $(this).attr('name')
];

jQuery AJAX文档:
你也可以寄这个

1) 作为
对象

var DTO = {
  id: $(this).attr('id'),
  id2: $(this).attr('id2'),
  name: $(this).attr('name')
};
2) 作为
数组

var DTO = [
    $(this).attr('id'),  
  $(this).attr('id2'),
  $(this).attr('name')
];

您可以编写一个函数,将给定jQuery节点的所有属性作为对象返回:

function getAttributes ( $el ) {
    var attributes = {};
    $.each( $el[0].attributes, function () {
        attributes[this.name] = this.value;
    });

  return attributes;
}
然后在
处理程序中使用它:

$('button').click(function(event) {
    event.preventDefault();
    $.ajax({
       type: "GET",
        url: "update_data.php",
        data:  getAttributes( $(this) )  // get all Attributes of the button
    });
});

// getAttributes( $(this) ) returns:
{
  class: "button_red",
  id: "i",
  id2: "id2",
  name: "name",
  type: "button"
}

建议是,无论元素上的属性有多复杂,您都可以在任何需要的地方轻松地重用这些数据,缺点是您可能已经注意到,您无法完全控制数据,添加了未指定的
类型
属性-它只返回所有属性。

您可以编写一个函数,将给定jQuery节点的所有属性作为对象返回:

function getAttributes ( $el ) {
    var attributes = {};
    $.each( $el[0].attributes, function () {
        attributes[this.name] = this.value;
    });

  return attributes;
}
然后在
处理程序中使用它:

$('button').click(function(event) {
    event.preventDefault();
    $.ajax({
       type: "GET",
        url: "update_data.php",
        data:  getAttributes( $(this) )  // get all Attributes of the button
    });
});

// getAttributes( $(this) ) returns:
{
  class: "button_red",
  id: "i",
  id2: "id2",
  name: "name",
  type: "button"
}

建议是,无论元素上的属性有多复杂,您都可以在任何需要的地方轻松地重用这些数据,缺点是您可能已经注意到,您无法完全控制数据,添加了未指定的
类型
属性-它只返回所有属性。

欢迎使用SO。第一步,整理缩进。这是一个很好的习惯,使事情变得更容易(也有助于我们的帮助)。其次,
data
是对象时最简单、最可读/可编辑的,因此我建议构建一个数据对象,然后将其提供给
data
。在javascript中,它将是
JSON.stringify()
not serialize()。欢迎使用。第一步,整理缩进。这是一个很好的习惯,使事情变得更容易(也有助于我们的帮助)。其次,
data
是一个对象时最简单、最可读/可编辑的,因此我建议构建一个数据对象,然后将其提供给
data
。在javascript中,它将是
JSON.stringify()
not serialize()。