jQuery的 - 如何获取类型的输入值ajaxifying一个表单时提交 [英] jQuery - how to get the value of an input of type submit when ajaxifying a form

查看:147
本文介绍了jQuery的 - 如何获取类型的输入值ajaxifying一个表单时提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下的code:

Suppose I have the following code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd"
    >
<html lang="en">
<head>
    <title><!-- Insert your title here --></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">

        $(document).ready(function(){
            $('#form1').submit(function(){

                // Let's see the values
                alert($(this).serialize());

                $.post($(this).attr('action'), $(this).serialize(), function(data){

                    // Do something on callback

                });

                return false;
            })
        });

    </script>
</head>
<body>
    <form action="http://example.com/bla" method="POST" id="form1">
        <input type="hidden" name="ipt1" value="1"/>
        <input type="submit" name="sbt" value="2"/>
    </form>
</body>
</html>

当我序列化形式的价值观点击提交输入类型的价值是不存在。如何ajaxifying我的形式,当我处理这种情况?

When I serialize the form's values the value of the input type submit clicked is not there. How do I handle this kind of situation when ajaxifying my forms?

推荐答案

在非Ajax表单提交按钮的值只与形式发送的,如果它被点击。所以,如果你想模仿使用Ajax,你必须做这样的事情这种行为:

In non-ajax forms the value of the submit button is only sent with the form, if it is clicked. So if you want to mimic this behaviour with ajax you have to do it something like this:

$("#submit_button").click(function() {
    var data = $("#my_form").serialize();
    data += data.length ? "&" : ""; // check if empty
    data += escape($(this).attr("name"))+"="+escape($(this).val());
    // ...
    return false;
});

这追加提交按钮的串行数据序列化格式的数据。

This appends the serialized data of the submit button to the serialized form data.

这篇关于jQuery的 - 如何获取类型的输入值ajaxifying一个表单时提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆