如何在新窗口中创建HTML表单 [英] How to create HTML Form in a new window

查看:93
本文介绍了如何在新窗口中创建HTML表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下功能:

// open a new window. Create and submit form
function createSubmitForm(response) {
    var external = window.open('about:blank', 'external');
    var doc = external.document;
    var body = $('body', doc);

    // create a new form element
    var form = $("<form id='authForm'></form>");
    // set the form's attributes and add to iframe
    form.attr("action", response.action)
        .attr("method", response.method);
    form.appendTo(body);

    // create form elements;
    for (var i = 0, len = response.fields.length; i < len; i++) {
        var field = response.fields[i];

        if (field.name != "") {
            $("<input type='hidden'/>")
                .attr("name", field.name)
                .attr("value", field.value)
                .appendTo(form);
        }
    }
    // submit the form
    form.submit();
}

当我尝试执行它时,在form.appendTo(body)处出现一个未指定错误" .

When I try to execute it, I get an "Unspecified Error" at form.appendTo(body).

我在这里怎么可能做错了?

What could I be doing wrong here?

推荐答案

尝试一下:
请注意,您的服务器上必须有一个空白页 http://localhost/some -blank-page-on-your-server.php

Try this:
Please notice that You must have a blank page on Your server http://localhost/some-blank-page-on-your-server.php

<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
  article, aside, figure, footer, header, hgroup, 
  menu, nav, section { display: block; }
</style>
</head>
<body>

</body>
<script>
(function($) {
$(function() {
    // open a new window. Create and submit form
    function createSubmitForm(response) {
        var external = window.open('http://localhost/some-blank-page-on-your-server.php', 'external');
setTimeout(function() {
        var doc = external.document;
        var body = $('body', doc);

        // create a new form element
        var form = $("<form id='authForm'></form>");
        // set the form's attributes and add to iframe
        form.attr("action", response.action)
            .attr("method", response.method);


        // create form elements;
        for (var i = 0, len = response.fields.length; i < len; i++) {
            var field = response.fields[i];

            if ( field.name !== "" ) {
                $("<input type='text'/>")
                    .attr("name", field.name)
                    .attr("value", field.value)
                    .appendTo(form);
            }
            $("<input type='submit' value='submit' />").appendTo(form);
        }
        // submit the form
        form.submit();

        body.append(form);
}, 1000);
    }

    createSubmitForm({
      action: 'http://www.example.com',
      method: 'get',
      fields: [{
        name: 'field1',
        value: 'some value'
      }]
    });
});
})(jQuery);
</script>
</html>

这篇关于如何在新窗口中创建HTML表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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