Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.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和Ajax循环元素_Jquery_Ajax - Fatal编程技术网

使用jquery和Ajax循环元素

使用jquery和Ajax循环元素,jquery,ajax,Jquery,Ajax,我有一个由类和数据-自定义属性标识的项目列表,如下所示: <div class="matrix_type" id="12" data-matrix-value="7"></div> <div class="matrix_type" id="189" data-matrix-value="4"></div> <div class="matrix_type" id="12090" data-matrix-value="10"></di

我有一个由类和数据-自定义属性标识的项目列表,如下所示:

<div class="matrix_type" id="12" data-matrix-value="7"></div>
<div class="matrix_type" id="189" data-matrix-value="4"></div>
<div class="matrix_type" id="12090" data-matrix-value="10"></div>
<div class="matrix_type" id="1234" data-matrix-value="2"></div>

我想使用一个按钮来触发AJAX post
Send

单击按钮后,我希望将每个“div”与其在AJAX请求中发送的对象中的值与其他数据相关联


有任何指南吗?

假设您希望获取所有div,并在将所有值发布到“一”对象之前迭代获取所有值,请执行以下操作:

$('#send_matrix').click(function(){

    // We post this to the server
    var postObject = {};

    //Get all the divs with the class of 'matrix_type' and iterate through
    $('.matrix_type').each(function(){

        //Get the id of the current div (please make them unique!)
        var id = $(this).attr('id');

        //Get the matrix value of the current div
        var matrixValue = $(this).data('matrix-value');

        //Add a new key-value pair to the postObject
        postObject[id] = matrixValue;
    });


    //Replace this with the url you post the data to
    var url = 'www.something.com' 

    //Post the data to the server
    $.post(url, postObject, function(data, status){

            //Show the result of the attempted post (success or failure)
            alert("Data: " + data + "\nStatus: " + status);
    });
});