我有一个类似下面的列表:
<p class='info' data-info='1'>Info 1 <span class='status'></span></p>
<p class='info' data-info='2'>Info 2 <span class='status'></span></p>
<p class='info' data-info='3'>Info 3 <span class='status'></span></p>......etc
我需要做的是遍历所有.info
元素,如果data-info
中包含myArray
号码,则更改范围文本。如何在jQuery中匹配数组内部的值?
答案 0 :(得分:3)
$('.info').each(function() {
$this = $(this);
if ($.inArray($this.data('info'), myArray) !== -1) {
$this.children('.status').text('New text value');
}
});
答案 1 :(得分:2)
你需要做这样的事情:
$(".info").each(function() {
if($(this).data('info').val() == <INSERT NUMBER TO MATCH>)
{
//Do some code here
//example
$(this).find("span").text("Some text here")
}
});
答案 2 :(得分:1)
如下所示,您可以获取每个'p'的数据信息值,之后您可以检查数组中是否存在此值
<p class='info' data-info='1'>Info 1 <span class='status'></span></p>
<p class='info' data-info='2'>Info 2 <span class='status'></span></p>
<p class='info' data-info='3'>Info 3 <span class='status'></span></p>......etc
var info = []
$('p').each(function(){
info.push($(this).attr('data-info'));
});
alert(info)
答案 3 :(得分:1)
不是循环遍历.info
元素,而是循环遍历myArray
。
for (var i = 0; i < myArray.length; i++) {
$("p[data-info=" + myArray[i] + "] .status").html("Your text");
}
答案 4 :(得分:1)
var valueToFind = ['3', '0', '1'];
$('.info').each(function(){
var infoData = $(this).data('info');
for(i=0;i<valueToFind.length;i++){
if(valueToFind[i]==infoData){
$(this).find('.status').html('found');
}
}
});
会做神奇的事。
这是一个实用的小提琴示例http://jsfiddle.net/mayooresan/fw94c/
答案 5 :(得分:0)
你去吧
实例http://jsfiddle.net/mithun/5Ryu5/3/
var infoSpecialArray = [2, 3, 4, 5];
$("p.info").each(function() {
if($.inArray(parseInt($(this).data('info'), 10), infoSpecialArray) != -1) {
$(this).find('span.status').html('special')
}
});