javascript - 如何返回自定义表单字段的值

标签 javascript json

这是我的代码:

<script src="https://sdk.paylike.io/3.js"></script>
<script>
    var paylike = Paylike('***');
    document.querySelector('button').addEventListener('click', pay);

    function pay(){
        paylike.popup({
            // locale: 'da',  // pin popup to a locale
            title: 'Product',
            description: '<?php echo $amount; ?>',
            currency: 'GBP',
            amount: <?php echo $price; ?>,
            // saved on transaction for retrieval from dashboard or API
            custom: {
                // arrays are fine
                products: [
                    // nested objects will do
                ],
            },
            // data from fields will be merged with custom
            fields: [
                // elaborate custom field
            {
                    name: 'name',
                    type: 'name',
                    placeholder: 'John Doe',
                    required: true,
                },

                {
                    name: 'email',
                    type: 'email',
                    placeholder: 'john@example.com',
                    required: true,
                },

                {
                    name: 'address',
                    type: 'address',
                placeholder: 'Address',
                    required: true,
                },
                {
                    name: 'postcode',
                    type: 'postcode',
                    placeholder: 'Postcode',

                    required: true,
                },

            ],
        }, function( err, res ){
            if (err)
                return console.log(err);
            console.log(res);



              location.href = 'success.php?e=' + email;
        });
    }
</script>

我希望能够获取在此弹出表单中提交的电子邮件,以便我可以将其附加到成功 URL,从而发送电子邮件收据。然而,我的尝试似乎并没有奏效。

这是控制台的输出:

Object
custom: Object
address: "116"
email: "test@gmail.com"
name: "John Doe"
postcode: "E9 7SR"
transaction: Object
id: "581b23717cb2057463e8d76a"

如何返回电子邮件对象?

预先感谢您的帮助。

最佳答案

尝试:

res.custom.email

翻译为:

location.href = 'success.php?e=' + res.custom.email;

我认为你应该使用 encodeURIComponent 来转义该值。电子邮件对于 URL 来说可能不是问题,但我认为这是一个好习惯:

location.href = 'success.php?e=' + encodeURIComponent(res.custom.email);

下次遇到类似问题时,请尝试将 debugger; (断点)放入代码中(在此示例中,紧接在 }, function( err, res ){ ),在 Google Chrome 中启动开发人员工具,重新加载,然后脚本将运行但在断点处停止,允许您在该确切点检查变量及其值。

关于javascript - 如何返回自定义表单字段的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40392500/

相关文章:

javascript - 将元素从一个区域拖动到另一区域

ruby - 解析 100mb JSON 负载的有效方法

c# - 如何在代码隐藏中处理 json 对象

javascript - 在 ExtJS 中提交响应有问题(在 JSON 中响应,但在某处输入了 HTML)

javascript - 无需将鼠标悬停在 WordPress 中的 Image Accordion Gallery 上即可显示标题

javascript - ionic 2 中的 Webpack 和 Web Workers

javascript - 单击饼图切片时,Bootstrap 模式不显示

python - 如何解析具有相同键的多个字典值

php - 使用 php 将多个值返回到带有 json 的 jquery.ajax

javascript - 使用Json数据源实现数据表中的复选框选择