如何在jQuery中获取具有特定父类的所有元素的内容,并将它们传递给get变量?

时间:2022-11-28 08:58:55

I need to collect the contents of all list items with the parent class of list_array, then pass them via a GET variable to an AJAX call. Can you recommend a strategy?

我需要使用list_array的父类收集所有列表项的内容,然后通过GET变量将它们传递给AJAX调用。你能推荐一种策略吗?

Here's a fiddle.

这是一个小提琴。

HTML:

HTML:

<div>
    <ul class="list_array">
        <li>item A</li>
        <li>item B</li>
        <li>item C</li>
    </ul>
</div>
<p>some text</p>
<div>
    <ul class="list_array">
        <li>item B</li>
        <li>item C</li>
        <li>item D</li>
    </ul>
</div>
<p>some text</p>
<div>
    <ul class="list_array">
        <li>item A</li>
        <li>item C</li>
        <li>item E</li>
    </ul>
</div>

Here's my current progress:

这是我目前的进展:

$(document).ready(function(){
    var listItemArr = [];
    $('.list_array li').each(function() {
        listItemArr.push($(this).text());
    });
    alert(listItemArr);
});

​ As the fiddle shows, this doesn't work.

正如小提琴所展示的,这是行不通的。

Ideally I would just pass unique strings as well, so the expected result would be:

理想情况下,我也会传递唯一的字符串,所以期望的结果是:

item A
item B
item C
item D
item E

(and not contain the duplicates)

(不含复制品)

Also, any recommendation in passing the array to my PHP processing page is welcomed.

此外,欢迎将数组传递到PHP处理页面的任何建议。

Thanks!

谢谢!

3 个解决方案

#1


1  

Try this:

试试这个:

var arr = [];
$('.list_array li').each(function() {
    var t = $(this).text();
    if (arr.indexOf(t) === -1) {
        arr.push(t)
    }
})
// arr = arr.join(',')  
// =>  item A,item B,item C,item D,item E

DEMO

演示

You can use the jQuery $.post or $.ajax utility functions for sending the data to PHP:

您可以使用jQuery $。职位或美元。将数据发送到PHP的ajax实用函数:

  1. $.post()
  2. $ . post()
  3. $.ajax()
  4. $ . ajax()
  5. $.get()
  6. $ . get()

#2


1  

Don't know about PHP. Here is the client side code:

不知道PHP。这是客户端代码:

var arrayName = 'whatever';
//use an object as an SET to remove duplicated items
var dict = {};
$('.list_array li').map(function() {
    return $(this).text();
}).each(function(){
    dict[this] = '';
});
//retrieve the values saved to the SET
var params = '';
for(var k in dict)
    params += arrayName + '=' + k +'&';
//send request
$.get('/path/to/your.php', params, function (response) {
    console.log(response);
    //or do what ever you want with the response.
}, 'html');

#3


1  

Try this

试试这个

var all=[];
$('ul.list_array li').each(function(){
    var text=$(this).text();
    if($.inArray(text, all)==-1) all.push(text);
});
$.get('/url/to/page', {'items': all}, function(data){
    console.log(data);
});

variable all is an array like ["item A", "item B", "item C", "item D", "item E"] and your array is available in $_GET as $_GET['items'].

变量all是一个数组,比如["item A", "item B", "item C", "item D", "item E"],你的数组可以在$_GET中作为$_GET['items']。

Check this to see the filtered array.

检查这个以查看过滤后的数组。

#1


1  

Try this:

试试这个:

var arr = [];
$('.list_array li').each(function() {
    var t = $(this).text();
    if (arr.indexOf(t) === -1) {
        arr.push(t)
    }
})
// arr = arr.join(',')  
// =>  item A,item B,item C,item D,item E

DEMO

演示

You can use the jQuery $.post or $.ajax utility functions for sending the data to PHP:

您可以使用jQuery $。职位或美元。将数据发送到PHP的ajax实用函数:

  1. $.post()
  2. $ . post()
  3. $.ajax()
  4. $ . ajax()
  5. $.get()
  6. $ . get()

#2


1  

Don't know about PHP. Here is the client side code:

不知道PHP。这是客户端代码:

var arrayName = 'whatever';
//use an object as an SET to remove duplicated items
var dict = {};
$('.list_array li').map(function() {
    return $(this).text();
}).each(function(){
    dict[this] = '';
});
//retrieve the values saved to the SET
var params = '';
for(var k in dict)
    params += arrayName + '=' + k +'&';
//send request
$.get('/path/to/your.php', params, function (response) {
    console.log(response);
    //or do what ever you want with the response.
}, 'html');

#3


1  

Try this

试试这个

var all=[];
$('ul.list_array li').each(function(){
    var text=$(this).text();
    if($.inArray(text, all)==-1) all.push(text);
});
$.get('/url/to/page', {'items': all}, function(data){
    console.log(data);
});

variable all is an array like ["item A", "item B", "item C", "item D", "item E"] and your array is available in $_GET as $_GET['items'].

变量all是一个数组,比如["item A", "item B", "item C", "item D", "item E"],你的数组可以在$_GET中作为$_GET['items']。

Check this to see the filtered array.

检查这个以查看过滤后的数组。