从按钮附加ID单击到现有的jQuery函数,该函数可切换表单

发布于 2025-02-08 12:08:46 字数 523 浏览 1 评论 0原文

我有一个表,每行都以动态生成的按钮结束。该按钮的ID与DB的用户ID匹配。我希望能够按任何按钮并将该ID传递给切换表单的脚本。我只是无法确定如何将ID附加到现有脚本以切换表单上。

到目前为止,我拥有的代码是

<button type='submit' class='btn btn-success' id='formButton$uid' onclick='myFunction()'>Edit User #$uid</button>

&amp;

$(document).ready(function() {
        $("#3").click(function() {
    $("#form1").toggle();
  });
});

如您在上面的代码中所看到的,我对3中的ID进行了硬编码,但是正如我所说的那样,我正在寻找该ID以从任何按钮中获取值,ID,因此,如果有50个按钮,则可以全部切换。按钮单击ID时的表单将附加到jQuery切换函数中。

I have a table, each row is ended with a dynamically generated button. That button has an id matching the id of the user from the DB. I want to be able to press any of the buttons and pass that id to a script that toggles a form. I just cannot work out how to append the id to an existing script to toggle the form.

So far the code I have is

<button type='submit' class='btn btn-success' id='formButton$uid' onclick='myFunction()'>Edit User #$uid</button>

&

$(document).ready(function() {
        $("#3").click(function() {
    $("#form1").toggle();
  });
});

As you see in the code above I have hard coded the id of 3 in but as I say I am looking for that id to take the value, the id, from any of the buttons clicked so if there are 50 buttons they can all toggle the form as the button clicked id will have be appended into the JQuery toggle function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自演自醉 2025-02-15 12:08:46

几个小时后,我设法将ID添加到jQuery中。

     <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="POST">
        <input type="submit" name="mybutton" id="1" value="1">
        <input type="submit" name="mybutton" id="2" value="2">
        <input type="submit" name="mybutton" id="3" value="3">
        <input type="submit" name="mybutton" id="4" value="4">
    </form>

$(document).ready(function() {
  $("#<?php echo $_POST['mybutton'] ?>").click(function() {
   $("#form1").toggle();
 });
});

我不知道这是否是完成这项工作的最佳方法?

After a few hours I have managed to add the id to the JQuery.

     <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="POST">
        <input type="submit" name="mybutton" id="1" value="1">
        <input type="submit" name="mybutton" id="2" value="2">
        <input type="submit" name="mybutton" id="3" value="3">
        <input type="submit" name="mybutton" id="4" value="4">
    </form>

Then the JQuery

$(document).ready(function() {
  $("#<?php echo $_POST['mybutton'] ?>").click(function() {
   $("#form1").toggle();
 });
});

I don't know if this is the best way of doing the job?

少女的英雄梦 2025-02-15 12:08:46

您可以在元素名称上挂起单击事件,例如

<button type='submit' name="mybutton" Id="#$uid" onclick='myFunction()'>Edit User #$uid</button>
<button type='submit' name="mybutton" Id="#$uid" onclick='myFunction()'>Edit User #$uid</button>
<button type='submit' name="mybutton" Id="#$uid" onclick='myFunction()'>Edit User #$uid</button>
$('button[name="mybutton"]').each(function() {
  $(this).click(function() {
    console.log($(this).attr("Id"));
    $("#form1").toggle();
  });
});

[https://jsfiddle.net/bjq5drwh/2/]

我希望这会有所帮助

you could hook the on click event on the element name like so

<button type='submit' name="mybutton" Id="#$uid" onclick='myFunction()'>Edit User #$uid</button>
<button type='submit' name="mybutton" Id="#$uid" onclick='myFunction()'>Edit User #$uid</button>
<button type='submit' name="mybutton" Id="#$uid" onclick='myFunction()'>Edit User #$uid</button>
$('button[name="mybutton"]').each(function() {
  $(this).click(function() {
    console.log($(this).attr("Id"));
    $("#form1").toggle();
  });
});

[https://jsfiddle.net/bjq5drwh/2/]

I hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文