2014-10-08 40 views
-1

我在一个页面上有多个表格,并且有一个从array_keys开始的数字数组。隐藏<tr>包含特定ID的

我已经给每个<tr>一个增量ID,从0开始。如果我的数组返回0,1,3,5我想<tr>的ID为2和4来隐藏。通常我会使用CSS并应用样式:display = none。

我相信我需要一个阵列内循环使用jQuery .find()像这样:

$arr = array_keys($floor_item, $po); //floor_item is my array & po is the value I'm searching 

foreach($arr as $key => $item){ 
print "<tr id='" . $item . "'><td>" . $item . "</td></tr>"; //this will show me the id's I want 

//just guessing here 
$("tr").find("$item").hide(); 

} 
+1

只有数字ID是一个HTML 5功能...最好你前缀他们和前缀匹配使用'$('tr [id^= myprefix]') – 2014-10-08 15:15:50

+1

你不能在php代码中使用jQuery。 – vaso123 2014-10-08 15:30:06

回答

1

如果您需要使用客户端代码隐藏元素,尝试这样的事情:

var jsArray = [<?php echo(implode(",", $ids_you_want_to_show_array); ?>]; 
$("#your_table_id tr").hide(); 
$.each(jsArray, function(key, value) { 
    $("#tr_" + value).show(); 
}); 

假设你的td的id是“tr_0”,“tr_1”等等。顺便说一下,不要使用数字作为ID。

+0

我可以得到此代码工作,但只能在页面上的第一个表。我已经使用了代码:'<?php $ floor_item = unserialize($ row ['floor_item']); \t $ tr_ids = array_keys($ floor_item,$ po); ? \t> ' – tomantford 2014-10-08 15:53:42

0

试试这个:

$arr = array_keys(1,2,3,4,5,6,7,8,9,10); 
$i == 0; 
foreach ($arr as $item) { 
    if ($i%2 != 0 || $i == 0) { 
     //this will show me the id's I want 
     print "<tr id='pref_" . $item . "'><td>" . $item . "</td></tr>"; 
    } 
    $i++; 
} 

无论如何,你需要学会从基础的PHP您开始使用之前。

在阵列中,如果你想键/值对,你可以将它定义成:

$array = array(
    'key1' => 'value', 
    7 => 'other value', 
    ... 
); 

$array['key1'] = 'value'; 
$array[7] = 'other value'; 

正如我提到的,你不能在使用jQuery/javascript代码你php代码。