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

报告相同问题?

悬赏问题

  • ¥50 swiftui @query 报错
  • ¥50 怎么解决刷卡或扫码后,点击软件输入框,win10屏幕键盘不会自动弹出的问题
  • ¥15 如何使用arcgispro的训练深度模型,发现water和nowater精度为0?(相关搜索:深度学习)
  • ¥20 matlab作业不太懂呀有问题能给个代码吗
  • ¥15 自制电路图为何无法驱动ESP01S?
  • ¥15 前端加access数据库
  • ¥15 ARCGIS 多值提取到点 ERROR 999999
  • ¥15 mysql异常断电, [MY-011971] [InnoDB]
  • ¥15 uni.onBluetoothDeviceFound熄屏不运行
  • ¥15 求PHDA糖尿病并发症数据集,有偿