【问题标题】:Hide <tr> containing specific ID's隐藏包含特定 ID 的 <tr>
【发布时间】:2014-10-08 23:36:43
【问题描述】:

我在一页上有多个表格和来自array_keys 的一组数字。

我为每个&lt;tr&gt; 赋予了一个递增的ID,从0 开始。如果我的数组返回0、1、3、5,我希望隐藏ID 为2 和4 的&lt;tr&gt;。通常我会使用 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();

}

【问题讨论】:

  • 纯数字 ID 是 HTML 5 的一项功能...最好为它们添加前缀并使用 `$('tr[id^=myprefix]') 匹配前缀
  • 你不能在php代码中使用jQuery。

标签: javascript php jquery html arrays


【解决方案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。

【讨论】:

  • 我可以让这段代码工作,但只适用于页面上的第一个表格。我使用了代码:&lt;?php $floor_item=unserialize($row['floor_item']); $tr_ids = array_keys($floor_item, $po); ?&gt; &lt;script&gt; $(document).ready(function(){ var jsArray = [&lt;?php echo(implode(",", $tr_ids)); ?&gt;]; console.log(jsArray) $("#blank &gt; tbody &gt; tr").hide(); $.each(jsArray, function(key, value) { $("#tr_" + value).show(); }); }); &lt;/script&gt;
【解决方案2】:

试试这个:

$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';

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

【讨论】:

    猜你喜欢
    • 2018-02-28
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    • 2018-07-05
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 2023-01-12
    相关资源
    最近更新 更多