将表单提交的值附加为url参数

时间:2019-09-04 17:20:26

标签: jquery

我正在尝试将表单提交的值作为url参数url追加到感谢页面。我该怎么办?

$('#bootstrapForm').submit(function (event) {
    event.preventDefault()
    var extraData = {}
    $('#bootstrapForm').ajaxSubmit({
        data: extraData,
        dataType: 'jsonp',  // This won't really work. It's just to use a GET instead of a POST to allow cookies from different domain.
        error: function () {
            // Submit of form should be successful but JSONP callback will fail because Google Forms
            // does not support it, so this is handled as a failure.

            // You can also redirect the user to a custom thank-you page:
             window.location = 'http://www.rositarococo.com/gracias.html'
        }
    })
}) 

1 个答案:

答案 0 :(得分:0)

因此,您需要一种将extraData对象转换为URL参数字符串的方法?一种方法是:

var queryString = Object.keys(extraData).map(key => key + '=' + params[key]).join('&');

或者,您也可以使用jquery的serialize()方法直接访问表单中的输入字段:

var querString = $("form").serialize()

(如果页面上有多个表格,您可能需要使选择器更加具体。)

在两种情况下,您都只需将其附加到您的URL:

window.location = 'http://www.rositarococo.com/gracias.html?'+queryString