douxian1939 2015-10-20 18:39
浏览 62

按下时有没有办法获取按钮的ID?

So basically, I'm coding a page that will have multiple divs (depending on the result of an SQL request, but that doesn't really matter) that all got the same structure (some text and two buttons), I need to pass a variable into this div to get it later.
So I had the idea of putting that variable into the IDs of both buttons (using php), which works fine.

 type=\"submit\" id=".$id_note." name=\"delete\" value=\"Delete\"> //id_note=my variable  
I know that it may not sound very clear, but that's the best I can do.
Now, my question is, how do I get this ID value, in php if possible, when the button is pressed ?
  • 写回答

1条回答 默认 最新

  • dongzhang1987 2015-10-20 18:49
    关注

    Bind events to each button and then get the id attribute from the clicked button

    $('button').click(function () {
        var id = $(this).attr('id');
    });
    

    If you have other buttons on your page, you should add a class to the buttons and then bind the event to that class instead.

    Edit: Read too quickly.

    You cannot retrieve the id of the button in your PHP when the form has been submitted. It only processes the input elements with a name, in to an associative array you can access the value the input field had. e.g $_POST['form_name']

    So as in the comments, best solution would be to use a hidden field that contains the id

    <input type="hidden" name="id_note" value="<?php echo $id_note; ?>">
    

    And then when submitted

    $_POST['id_note']; // --> id_note value
    
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据