doubi1818 2016-07-12 13:29
浏览 51
已采纳

使用jQuery检索表单中的提交按钮的名称

I am working with PHP and jQuery. I have 2 files

test.php

<script>
    $(document).ready(function(){
        $(".form-register").submit(function (e) {

            var form_data = $(this).serialize();

            e.preventDefault();

            $.ajax({
                type: "POST",
                url: "test2.php",
                data: form_data,
                dataType: 'json',
                success: function(){
                    alert(form_data);
                }

            });
        });


    });
</script>

<form class="form-register">
    <input name="email" type="text"/>
    <input name="name" type="text"/>
    <input type="submit" name="register"/>
</form>

and the second file is test2.php:

<?php
if(isset($_POST['register'])){
    echo json_encode('Message from test2.php');
}else{
    echo json_encode('post no received');
}

It seems like I am unable to retrieve $_POST['register'] because when I check alert(form_data); only the email and the name are displayed.

Is there anyway for me to get $_POST['register']?

</div>
  • 写回答

2条回答 默认 最新

  • douyong2531 2016-07-12 14:04
    关注

    Neither .serialize() nor .serializeArray() will serialize the submit button value.

    .serialize()

    ...No submit button value is serialized since the form was not submitted using a button.

    .serializeArray()

    ...No submit button value is serialized since the form was not submitted using a button.

    Only successful controls are serialized.

    The input submit element is actually a successful control, but as mentioned above since the form is not submitted using the submit button, the input submit value attribute will not be included by jquery serialization.

    If you want to use .serializeArray() just add the input submit elements name and value attribute values as an object to the serialized array like so.

    $(document).ready(function(){
      $(".form-register").submit(function (e) {
    
        var form_data = $(this).serializeArray();
        var $submit = $(this).find('input[type="submit"]');
        form_data.push({name: $submit.prop("name"), value: $submit.val()});
        console.log(form_data);
        
        // do your ajax request here...
        
      });
    
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <form class="form-register">
      <input name="email" type="text"/>
      <input name="name" type="text"/>
      <input type="submit" name="submit" value="register"/>
    </form>

    If you want to use .serialize() just add the values of the value and name attributes of the submit element to the text string created by the .serialize() method. Be careful, the string needs to be URL-encoded.

    $(document).ready(function(){
      $(".form-register").submit(function (e) {
    
        var form_data = $(this).serialize();
        var $submit = $(this).find('input[type="submit"]');
        form_data = form_data + "&" + $submit.prop("name") + "=" + $submit.val();
        console.log(form_data);
    
        // do your ajax request here...
    
      });
    
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <form class="form-register">
      <input name="email" type="text"/>
      <input name="name" type="text"/>
      <input type="submit" name="submit" value="register"/>
    </form>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏
  • ¥15 STM32串口接收问题