在提交按钮后添加表单输入

时间:2014-04-16 11:36:34

标签: javascript php html forms input

我有以下javascript函数

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>

当脚本运行时,我将表单作为

<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" />位于提交按钮之后,因此该字段未提交。

如何在提交按钮之前附加此字段,有任何帮助吗?

2 个答案:

答案 0 :(得分:0)

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

在您的表单中添加div一些id,例如<div id="div1"></div>,您的javascript就像

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);

如果您遇到任何其他问题,请告诉我......

答案 1 :(得分:-1)

您可以使用jQuery prepend 功能:

$("input[type='submit']").prepend('<input type="hidden" id="token" name="token" value="'+ token + '">');
$("input[type='submit']").prepend('<input type="hidden" id="cvv" name="cvv" value="'+ txCvv + '">');