inul php id,如何遍历UL中的LI项,获取特定属性,并通过$ .ajax数据传递它们(How to loop through LI items in UL, get specific att...

如何遍历UL中的LI项,获取特定属性,并通过$ .ajax数据传递它们(How to loop through LI items in UL, get specific attributes, and pass them via $.ajax data)

这是我的HTML代码:

这是我的样本ajax请求:

$.ajax({

url: '/ajax/upload.php',

type: 'POST',

data: { ... },

success: function(data){

}

});

这就是我想要实现的目标。

如果ID attr在那里,如何获取图库中每个LI的ID属性中的附件编号,并通过ajax数据以这种方式传递它们: { attached : '32,34,38,64,75' }如果有这是一个更好的方法,让我知道我想传递包含附件的列表项进行处理。 我想避免使用空的LI项,不包括id attr。

如何获取具有特色类的列表项LI {feature_img:..}并传递附件ID号(如果特征LI存在),如果没有列表项,则将features_img传递给0.所以我知道如何处理它在请求中通过PHP。

任何帮助表示赞赏。

谢谢。

Here is my HTML code:

Here is my sample ajax request:

$.ajax({

url: '/ajax/upload.php',

type: 'POST',

data: { ... },

success: function(data){

}

});

Here is what I want to achieve.

How to get the attachment number in ID attribute for every LI inside the gallery UL only when an ID attr is there and pass them via ajax data in this way: { attached : '32,34,38,64,75' } if there is a better way of doing this let me know I want to pass the list items which contain attachments to process. I want to avoid LI items which are empty, do not include id attr.

How to get the list item LI which has class of featured e.g. { featured_img : .. } and pass the attachment ID number if featured LI exists, and if none of list items is featured pass featured_img with 0. So i know how to process it via php in the request.

Any help is appreciated.

Thank you.

原文:https://stackoverflow.com/questions/10848747

更新时间:2020-02-22 19:02

最满意答案

有一个.map :

var ids = $("#gallery li").map(function () {

return this.id.split("-")[1];

}).get().join(",");

...

$.ajax({

url: '/ajax/upload.php',

type: 'POST',

data: { ids: ids },

success: function(data) {

}

});

There's an .map for that:

var ids = $("#gallery li").map(function () {

return this.id.split("-")[1];

}).get().join(",");

...

$.ajax({

url: '/ajax/upload.php',

type: 'POST',

data: { ids: ids },

success: function(data) {

}

});

2012-06-01

相关问答

试试:(如果你使用大于1.6.8的jQuery。) $(document).on('click','li:not(:first-child)',function() {

http://api.jquery.com/on/ Try with: (if you use jQuery greater than 1.6.8. ) $(document).on('click','li:not(:first-child)',function() {

http://api.jquery.com/on/

问题出在这里: mounted() { this.taskList = this.tasks; ...} 在TaskList.vue内部的mounted() { this.taskList = this.tasks; ...}作为taskList属性仅在已mounted事件上更新。 在Vue中有一个简单的解决方案:你应该让taskList成为一个依赖于props的computed property ,这样当父数据改变时你的计算属性就会被更新: props: ['tasks'],

computed:

...

我会做这样的事情: 首先,将数据作为数组取出,并为每个条目循环遍历。 然后运行这样的事情: $menuArray = array();

if (empty($type)) // If the entry has no "type", then it's a parent

{

$menuArray[$type]['title'] = $title;

}

else // else, it's a child, so append it to the parent

{

$menuArr

...

你需要这样的东西: var ul = $("#container");

for (var i = 0; i < json.cuy.length; i++) {

var txt = "

" + json.cuy[i].Title + "";

var parent;

if (json.cuy[i].IDparent === "0") {

parent = ul;

...

var sampleData = ['Product 1', 'Product 2', 'Product 3'];

function setSupplier() {

$('#supplier').val($(this).text());

}

$("#search").on('input keyup', function () {

var val = $(this).val().trim();

if (!val.length) {

$("#here").em

...

简单的事实是你使用#worlds-dropdown li而不是.worlds-dropdown li 。 我也不知道为什么你需要allOptions.toggle()因为它隐藏了li东西。 但我现在已经评论过了。 如果这是你的功能,你可以取消注释它。 $("#worldBtn").on("click", function() {

$('#displayWorlds').toggle();

});

var allOptions = $(".worlds-dropdown li");

...

有一个.map : var ids = $("#gallery li").map(function () {

return this.id.split("-")[1];

}).get().join(",");

... $.ajax({

url: '/ajax/upload.php',

type: 'POST',

data: { ids: ids },

success: function(data) {

}

});

演示。 There's an

...

用$(this)获取当前的ul ,然后找到那个ul里面的'li'并切换它们。 $('ul').on('click', function(){

$(this).find('li').fadeToggle("fast");

}); a{

text-decoration:none;

padding: 20px;

}

ul{

display:inline-block;

background-color:green;

padding:10px;

co

...

这是因为关闭。 Ajax成功回调中的内部函数创建一个闭包,并捕获变量i值。 内部函数包含对i变量的引用。 循环执行后, i的值为json.projects.length ,当你尝试访问json.projects[json.projects.length]你会得到undefined 。 要解决您的问题,您可以尝试使用let变量而不是var ,它与范围相比具有更短的范围。 您可以在这个问题中引用其他方法: 循环内部的JavaScript闭包 - 简单的实际示例 var json = JSON.pars

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值