Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript如何区分表单的一个按钮和另一个按钮?_Javascript_Php_Jquery_Twitter Bootstrap - Fatal编程技术网

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

javascript如何区分表单的一个按钮和另一个按钮?,javascript,php,jquery,twitter-bootstrap,Javascript,Php,Jquery,Twitter Bootstrap,下面是index.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>

下面是index.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>
这里是remote.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;
当我在表格中只有一行时,例如:Iron Maiden,我会收到带有相应信息的引导模式。当我在表格中有多行时,例如:Iron Maiden和Metallica,我只收到最后一行的结果。在本例中,Metallica也是在我单击第一行的按钮时收到的。如果我插入另一行并单击第一个或第二个按钮,我将收到带有最后一行信息的模态


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

对表单或按钮使用唯一id。然后给他们打电话:

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


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

为创建的每个按钮创建唯一的类值,以便您可以在jQuery中引用它。 在您的示例中,两者都是用“send”定义的。您可以在两个按钮的类attibute中添加另一个类值,并设置一个唯一值,如:

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

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

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

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

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

$('.form').submit(function(){
但是,问题在于通过ajax实际发送的内容,罪魁祸首如下:

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

因此,您需要做的是修改绑定submit事件的函数。你必须告诉函数,请序列化实际提交的表单。您可以通过以下方式进行:

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

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

确保两个按钮都有唯一的名称或id…为每个按钮使用唯一的id您可以为每个按钮分配类$.send1。单击函数{//do button actions here};你们中有人在漫无目的地谈论按钮之前花了30秒钟试图理解这个问题吗?@N.B.打得好!问题是,如果每个按钮都有不同的类或ID,那么我必须有很多js函数。如果我有一个有1000行的表,我需要1000个js函数,每个按钮一个。
$(function(){
        $('.form').submit(function(e){
            $.ajax({
                data: $(e.currentTarget).serialize(),
                type: 'POST',
                url: 'remote.php',
                success: function(data){
                    $('.result').html(data);
                }
            });
            return false;
        });
    });