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条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?