doulin6761 2016-06-28 20:43
浏览 73
已采纳

如何根据按下的按钮来调用函数来显示div?

I'm trying to generate a set of N buttons, one for each row of a DB table, I need to show a <div> by putting his style="display:none" to style="display:block" when one of the buttons is clicked, this <div> will show a form.

Until now i got this:

home.PHP

    <div id="add" style="display:none;">
        <form action="#" method="post" id="form">
            <div src="close" id="close" onClick="div_hide()">X</div>
            Aggiungi
            <input type="text" name="soldi" id="campo" placeholder="Soldi">
            <a id="submit" href="">
                <button>Aggiungi</button>
            </a>
        </form>
    </div>
<?php
    while($sezioni = mysqli_fetch_assoc($sezione_sql)){     
        echo "<div class =\"sezione\">
                  <a id=\"popup\" onclick=\"div_show()\">
                      <div class=\"pulsante\">add</div>
                  </a>
              </div>";
    }
?>

java.JS

function div_show() {
    document.getElementById('add').style.display = "block";
}

function div_hide(){
    document.getElementById('add').style.display = "none";
}

How could I call a function on submit that will update a determinated DB row getting in input an ID of the pressed button?

  • 写回答

1条回答 默认 最新

  • drq61040 2016-06-28 21:02
    关注
    1. Pass db_id through javascript function.
    2. In javascript function assign the passed db_id to the hidden input field in the form.

    Passing db_id:

    while($sezioni = mysqli_fetch_assoc($sezione_sql)){  
        $db_id = $sezioni['id']; // id from database   
            echo "<div class =\"sezione\">
                      <a id=\"popup\" onclick=\"div_show($db_id )\">
                          <div class=\"pulsante\">add</div>
                      </a>
                  </div>";
        }
    

    In Form add a hidden input field

    <input type="hidden" name="db_id" id="db_id" value="">
    

    In Javascript function

    function div_show(db_id) {
        document.getElementById('add').style.display = "block";
        document.getElementById('db_id').value = db_id;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作