【问题标题】:Display array value on click PHP单击 PHP 时显示数组值
【发布时间】:2013-05-19 10:03:24
【问题描述】:

让我们开始吧!

我做了一个我不想在我的网站上显示的 PHP 计算。棘手的部分是我希望显示值,每次用户单击按钮 ONCE 时都有 ONE 值。不管它是什么类型的按钮,我只是不想在每次点击时都显示一个值。值的总数为 8(固定长度),现在我有 4 个数组,每个数组有 2 个值。因此,每件事都已完成并保存,我只是想展示它,一次展示它。如果更容易,我可以将它们组合成 1 个数组。我试图制作一种display:none 与显示onclick 的东西,但它只在每次点击时显示第一个值。非常感谢提示和技巧!一种FORM 会帮助我吗?改用input

这就是我所拥有的:

HTML

<a href="#" class="modern" onClick="showhide('first', 'block'); return false" >Give me your value</a>

<div id="first"><?php echo $team1[0];?></div>
<div class="secound"><?php echo $team1[1];?></div>
<div class="third"><?php echo $team2[0];?></div>
<div class="fourth"><?php echo $team2[1];?></div>
...

Javascript

function showhide(divid, state){
 document.getElementById(divid).style.display=state
}

...

【问题讨论】:

    标签: php javascript html css


    【解决方案1】:

    有很多不同的方法可以做到这一点。使用框架可以让您更轻松轻松地支持多种浏览器,但假设这不是目标并尽可能多地坚持...

    我对 PHP 做了一些改动:

    <a href="#" id="team-next">Give me your value</a>
    <div id="teams">
      <div id="team-0"><?= $team[0][0] ?></div>
      <div id="team-1"><?= $team[0][1] ?></div>
      <div id="team-2"><?= $team[1][0] ?></div>
      <div id="team-3"><?= $team[1][1] ?></div>
      ...
    

    请注意,我已将 id 更改为前缀 team- 和索引 0 ... n,并且团队出现在具有 id 团队的父元素中。我还将数组更改为多维数组,而不是其中包含数字的多个变量。可以通过将数组添加为数组项来创建此构造。现在是 JavaScript(这应该出现在上述 HTML 之后的脚本标记中):

    // Initial index -1 (nothing displayed)
    var current = -1;
    
    // How many teams are there?
    var count = document.getElementById("teams").children.length;
    
    // The handler
    function showNext() {
    
      // A bit of modulus math, when it gets to the
      // same value as the count it starts at 0 again
      var next = (current + 1) % count;
    
      // Hide the previous one, unless it is the first
      if(current !== -1)
        document.getElementById("team-" + current).removeAttribute("style");
    
      // Show the next one
      document.getElementById("team-" + next).style.display = "block";
    
      // Update the current index
      current = next;
    } 
    
    // Bind the event listener
    document.getElementById("team-next").addEventListener("click", showNext, false);
    

    Here is a fiddle

    【讨论】:

    • 你知道当你为一个问题工作一整夜时,你会陷入困境和沮丧。然后你把它贴在这里,像@stuart 这样的人出现并在 Matrix 中形成像 Neo 一样的代码。十分感谢 !这正是我想要的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    • 1970-01-01
    • 2021-08-17
    • 2018-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多