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 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?