简体   繁体   English

AJAX请求来自具有相同类的多个div的多个值

[英]AJAX request for multiple values from multiple divs with same class

So, lets say that we have this structure: 因此,可以说我们具有以下结构:

  <div id='allTempLinkHolder'>
    <div class='tempLink'>Value</div>
    <div class='tempLink'>Value 2</div>
    <div class='tempLink'>Value 3</div>
 </div>

How i can submit all of the different values of .tempLink with post ajax, for example to post them to fetch.php ? 我如何使用post ajax提交.tempLink的所有不同值,例如将它们提交到fetch.php? Thanks! 谢谢!

var params;

$(".tempLink").each(function(){
  params=$.extend({}, params,   { "tmplink[]" , $(this).text() }  );
});

$.ajax({
    type: "POST",
    url: "url",
    data: params,
    success: function() {
      //Success code here
    }
});

in PHP you will receive it in an array $_POST["tmplink"] 在PHP中,您将在数组$_POST["tmplink"]收到它

Try this: 尝试这个:

http://jsfiddle.net/htKRf/3/ http://jsfiddle.net/htKRf/3/

var links = []

// Put all the tempLinks into an array. 
$.each($('.tempLink'), function (index, element) {
    links.push($(element).text());    
});

// Post it to the url.
$.post('fetch.php', JSON.stringify(links), function() { console.log("SUCCESS"); });

if you simmply want an array sent: 如果您只想发送一个数组:

var values = $.map($('.tempLink'), function(el, idx) {
    return $(el).text()
})

$.post('fetch.php', { values: values}, function(data) {
        /* do something with return data*/
})
var values=[];
$(".tempLink").each(function(){
    values.push($(this).text());
});

$.post({
    url: 'yourPhpScript.php',
    data: {values:values},
    success: function(data) {
        console.log(data);
    }
});

In your php script use 在您的php script使用

$arr = $_POST['values'];

$value1=$arr[0]; // value
$value2=$arr[1]; // value1
$value2=$arr[2]; // value3

Either a separate Ajax call for each div 为每个div单独调用Ajax

 $("div.tempLink").each(function(index, elem) {
    $.ajax({
        url: "my url" + "?param=" + this.text(),
        success: function() {...
        }
    });
});​

or a single call with array of values 或带有值数组的单个调用

var values = new Array();
$("div.tempLink").each(function(index, elem) {
    values.push(this.text());
});

$.ajax({
    type: "POST",
    url: "my url",
    data: JSON.stringify(values),
    contentType: "application/json; charset=utf-8",
    success: function() {...
    }
});​

First you would use a jQuery class selector to collect the values into a suitable data structure. 首先,您将使用jQuery类选择器将值收集到合适的数据结构中。 I wasn't sure how you wanted to do that, so I used a javascript map and linked the position in the original list to the value of the text {"link1":"value1","link2":"value2",...} . 我不确定您要怎么做,所以我使用了javascript映射并将原始列表中的位置链接到文本{"link1":"value1","link2":"value2",...}

var map={};
$(".tempLink").each(function(index) {
    map["tempLink"+index] = $(this).text();
});

To figure out the usage and syntax of $.ajax see the jquery ajax api ref. 要弄清楚$ .ajax的用法和语法,请参阅jquery ajax api ref。

$.ajax({
    type: "POST",
    url: "fetch.php",
    data: JSON.stringify(map),
    dataType: "...", //The type of data you're expecting the server to respond with
    contentType: "application/json; charset=utf-8",
    success: function(data, status, jqXHR) {},
    error: function(jqXHR, status, error) {}
});​

When your ajax call returns, if it's successful the success callback fires, otherwise error fires. 当您的ajax调用返回时,如果成功,则触发成功回调,否则触发错误。 You can use these to handle the data your server responds with, or let the user no that the request failed. 您可以使用它们来处理服务器响应的数据,或者让用户不要认为请求失败。

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

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