javascript 如何区分表单的一个按钮和另一个按钮


How can javascript distinguish one button of a form from another?

这里是索引的代码.php:

<html>
<head>
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <table>
        <tr>
            <td>Name</td>
            <td>Band</td>
            <td>Indx</td>
            <td>Send</td>
        </tr>
        <tr>
            <td>Bruce</td>
            <td>Iron Maiden</td>
            <td>95</td>
            <td>
                <form action="" class="form" method="POST">
                    <input class="name" name="name" type="hidden" value="Bruce">
                    <input class="band" name="band" type="hidden" value="Iron Maiden">
                    <input class="indx" name="indx" type="hidden" value="95">
                    <button class ="send" type="submit">SEND<button>
                </form>
            </td>
        </tr>
        <tr>
            <td>James</td>
            <td>Metallica</td>
            <td>90</td>
            <td>
                <form action="" class="form" method="POST">
                    <input class="name" name="name" type="hidden" value="James">
                    <input class="band" name="band" type="hidden" value="Metallica">
                    <input class="indx" name="indx" type="hidden" value="90">
                    <button class ="send" type="submit">SEND<button>
                </form>
            </td>
        </tr>
    </table>
    <div class="result"></div>
    <script src="https://code.jquery.com/jquery-1.11.1.js"></script>
    <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
    <script>
        $(function(){
            $('.form').submit(function(){
                $.ajax({
                    data: $('.form').serialize(),
                    type: 'POST',
                    url: 'remote.php',
                    success: function(data){
                        $('.result').html(data);
                    }
                });
                return false;
            });
        });
    </script>
</body>
</html>

这里是远程的代码.php

<?php
    $name = $_POST['name'];
    $band = $_POST['band'];
    $indx = $_POST['indx'];
    $up = $indx * 2;
    $output = '
        <div id="rock-modal" class="modal fade in" role="dialog" tabindex="-1" aria-labelledby="myLargeModalLabel" aria-hidden="true">
            <div class="modal-dialog modal-lg">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="myModalLabel">
                            <i class="fa fa-files-o"></i>
                            Rock Modal
                        </h4>
                    </div>
                    <div class="modal-body">
                        The band '.$band.' has an up index of '.$up.'!!!
                    </div><!-- /modal-body -->
                </div><!-- /modal-content -->
            </div><!-- /modal-dialog modal-lg -->
        </div><!-- /modal fade in-->
        <script type="text/javascript">
            $("#rock-modal").modal("show");
        </script>';
    echo $output;

当我在表中只有一行(例如:铁娘子(时,我会收到带有相应信息的引导模式。当我在表格中有不止一行时(例如:铁娘子和金属乐队(,当我单击第一行的按钮时,我也只收到最后一行(在本例中为金属乐队(的结果。如果我插入另一行并单击第一行或第二个按钮,我会收到带有最后一行信息的模态。

我怎样才能告诉javascript我到底在点击哪个按钮?

您遇到的问题来自具有多个表单,然后最终序列化错误的表单。

您将提交事件绑定到包含.form的任何内容,从而能够捕获任何形式的提交事件。我指的是这一行:

$('.form').submit(function(){

但是,问题在于您实际通过ajax发送的内容,罪魁祸首是以下行:

data: $('.form').serialize(),

当您有一个表单时,该行工作正常。当你有多个表单时,jQuery如何知道要序列化哪个表单呢?根据我的假设,它将选择LAST表单,因为选择器$('.form');将选择所有表单。它将序列化所有表单,但是当值到达 PHP 脚本时 - 由于字段名称在所有表单中都相同,传播的值将是最后一个表单的值。

因此,您要做的是修改绑定提交事件的函数。您必须告诉该函数"请序列化实际提交的表单"。您可以通过以下方式做到这一点:

$(function(){
        $('.form').submit(function(e){
            $.ajax({
                data: $(e.currentTarget).serialize(),
                type: 'POST',
                url: 'remote.php',
                success: function(data){
                    $('.result').html(data);
                }
            });
            return false;
        });
    });

请注意回调函数和data行的变化。这意味着你不必告诉javascript按下了哪个按钮。您要做的是序列化提交的表单。

对窗体或按钮使用唯一 ID。 然后用以下命令调用它们:

$('#form-one button') // if you set ID to form

$('#my-button-id') // if you set id to button.

ID 必须是唯一的,才能遵循 HTML 规则和 jQuery 才能工作

为每个创建的按钮创建唯一的类值,以便您可以在 jquery 中引用它。在您的示例中,两者都使用"发送"定义。您可以在两个按钮的类属性中添加另一个类值,并设置一个唯一值,如下所示:

<button class ="send button_1" type="submit">SEND<button>
<button class ="send button_2" type="submit">SEND<button>

另外,您可以使用唯一 id 来 jquery 执行特定操作。