本文介绍了jQuery POST不使用IE 11提交表单值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了几个小时的解决方案,但是以下代码根本无法在Internet Explorer 11中使用.它确实适用于Chrome和Firefox.使用IE11时,帖子已提交,但提交的表单为空.

I have tried to find a solution for this problem for hours now, but the following code is simply not working for Internet Explorer 11. It does work for Chrome and Firefox. When using IE11 the post is submitted but the submitted form is empty.

<!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
            <script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
        </head>
        <body>
            <form action="/Mandate/Edit" id="mandateForm" method="post">
                <input id="ExternalId" name="ExternalId" type="hidden" value="" />
                <input id="mandateName" name="mandateName" type="text" />
                <a href="#" id="md-submit">Create</a>
            </form>
            <script type="text/javascript">
                $(function () {
                    $("#md-submit").on("click", function (e) {
                        e.preventDefault();
                        var form = $("#mandateForm");
                        var request = $.ajax({
                            type: "POST",
                            url: form.attr("action"),
                            data: {
                                mandateName: "test4711"
                            },
                            dataType: 'json',
                            cache: false
                        });
                    });
                });
            </script>
        </body>
    </html>

非常感谢您的帮助.

推荐答案

serialize()方法不会将表单数据转换为Json ...

The serialize() method does not convert form data to Json...

这应该可行-IE11中的输出将为{"ExternalId":"","mandateName":"4343"}:

This should work--the ouput in IE11 will be {"ExternalId":"","mandateName":"4343"}:

<!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
            <script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
        </head>
        <body>
            <form action="/Mandate/Edit" id="mandateForm" method="post">
                <input id="ExternalId" name="ExternalId" type="hidden" value="" />
                <input id="mandateName" name="mandateName" type="text" />
                <a href="#" id="md-submit">Create</a>
            </form>
            <script type="text/javascript">
                function form_to_json (selector) {
                  var ary = selector.serializeArray();
                  var obj = {};
                  for (var a = 0; a < ary.length; a++) obj[ary[a].name] = ary[a].value;
                  return JSON.stringify(obj);
                }

                $(function () {
                    $("#md-submit").on("click", function (e) {
                        e.preventDefault();
                        var form = $("#mandateForm");
                        var request = $.ajax({
                            type: "POST",
                            url: form.attr("action"),
                            data: form_to_json(form),
                            dataType: 'json',
                            cache: false
                        });
                    });
                });
            </script>
        </body>
    </html>

这篇关于jQuery POST不使用IE 11提交表单值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 13:44