【问题标题】:How to pass the button ids of an array of buttons to php file?如何将按钮数组的按钮 ID 传递给 php 文件?
【发布时间】:2017-01-29 09:27:18
【问题描述】:

我想使用单个按钮创建新按钮,在创建这些新按钮后,我想单击它们并将它们的 ID 号发送到 php 文件。

我的代码:

<script>
var count=1;

   document.getElementById("btn").onclick=function(){
      document.getElementById("col").innerHTML+="<tr><td><button type='submit' name='id' id='clickable'>"+count+"</button></td></tr>";
      var cid=count;
      $("#clickable").click(function(){
         location.href="trial29.php?id="+cid;
      });
      count++;
   }

</script>

问题是我不能让多个按钮点击。 此外,我无法将按钮的 id 设置为它们所具有的值。

【问题讨论】:

  • id 是一个唯一标识符。拥有许多具有相同 id 的项目意味着只会考虑第一个。
  • 您应该在动态创建这些按钮时使用 'on('click',...'。考虑添加 jsfiddle 或更具体地描述错误。
  • 使用通用类代替id
  • 谢谢!是的,使用类工作
  • $("#clickable").click(function(){ location.href="trial29.php?id="+cid; });这个块有问题并导致代码停止,这就是为什么计数保持等于 1 并且没有增加

标签: javascript php jquery button


【解决方案1】:

使用此代码...

<script>
var count=1;

document.getElementById("btn").onclick = function(){

    document.getElementById("col").innerHTML+="<tr><td><button type='submit' name='id_"+count+"' class='newbtn' id='"+count+"'>"+count+"</button></td></tr>";
    var cid=count;
    count++;
    alert(count)

}

    $(document).on("click", ".newbtn", function(e) {
        var cid = $(this).attr('id');

        alert(cid);
        location.href="trial29.php?id="+cid;
    });

</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    • 2017-01-31
    • 2021-07-18
    相关资源
    最近更新 更多