在提交按钮后附加表单输入 [英] form inout getting appended after the submit button

查看:123
本文介绍了在提交按钮后附加表单输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下javascript函数

I have the following javascript function

txJ$(document).ready(function () {

    // Hide the error display, as there is currently no error
    txJ$("#TokenProxy_Error").css('display', 'none');

    //txJ$(".submit").closest("form").submit(function (e) {
    txJ$(".submit").closest("form").submit(function (event) {
        //check for encryption key
        { TxEncrypt(event); }
    });
});
function TxEncrypt(event)
{ //perform encryption of token data, then submit the form like normal

    //obtain public key and initial JSEncrypt object
    var txPubKey = 'jjh';
    var txEncrypter = new JSEncrypt();
    txEncrypter.setPublicKey(txPubKey);

    //get Data and encrypt it
    var txData = '{}';
    var txCryptData = '';
    if(txJ$(".data").length > 1)
    { //if there are more than one element with this class, convert it to json string
        txData = txJ$(".data").serializeObject();
        txCryptData = txEncrypter.encrypt(JSON.stringify(txData));
    }
    else
    {   //else, just encrypt the value
        txData = txJ$(".data").val();
        txCryptData = txEncrypter.encrypt(txData);
    }


    dataString = txCryptData;
    var xhr = new XMLHttpRequest();
    var params=dataString;
    var token;
    xhr.onreadystatechange = function() {
    if (xhr.readyState == 4 && xhr.status==200) {
    token=xhr.responseText;
    alert(token);
    //add value/field to form
    txCvv = txJ$(".cvv").val();

    var MyForm = txJ$(".zwitch_submit").closest("form");

        txJ$('<input type="hidden">').attr({
                id: 'token',
                name: 'token'
            }).val(token).appendTo(MyForm);
        txJ$('<input type="hidden">').attr({
                id: 'cvv',
                name: 'cvv'
            }).val(txCvv).appendTo(MyForm);

    //scrub data that we do not want to post
    txJ$(".data").removeAttr('name');
    txJ$(".cvv").removeAttr('name');
        }
    }
    xhr.open('POST', 'tokenize.php', false);
    xhr.send(params); 

html格式是

<form method="POST" action="pp.php">

<input type="text" class="data" name="ccnumber" value="4048341128241910" />
<input type="text" class="cvv" name="cvv" />



<input type="submit" class="submit"  value="tokenize" />

</form>

脚本运行时,将其显示为

when the script runs,im getting the form as

<form method="POST" action="pp.php">

<input type="text" class="data" name="ccnumber" value="4048341128241910" />
<input type="text" class="cvv" name="cvv" />



<input type="submit" class="submit"  value="tokenize" />
<input type="hidden" name="card_token" />

</form>

使用javascript附加的字段<input type="hidden" name="card_token" />位于提交按钮之后,因此该字段不会被提交.

The field <input type="hidden" name="card_token" /> which was appended using javascript comes after the submit button,so that field is not getting submitted.

我该如何在提交"按钮之前附加此字段?有没有帮助?

How can i append this field before the submit button,any help?

推荐答案

为什么不尝试这样的事情.

why dont you try something like this..

在您的表单中放入div和一些id,例如说<div id="div1"></div>,而您的JavaScript应该像

in your form put a div with some id like say <div id="div1"></div> and your javascript should be like

var MyForm = txJ$("#div1");

        txJ$('<input type="hidden">').attr({
            id: 'token',
            name: 'token'
        }).val(token).appendTo(MyForm);
    txJ$('<input type="hidden">').attr({
            id: 'cvv',
            name: 'cvv'
        }).val(txCvv).appendTo(MyForm);

如果您还有其他问题,请告诉我...

let me know if you face any other issue...

这篇关于在提交按钮后附加表单输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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