form inout getting appended after the submit button

﹥>﹥吖頭↗ 提交于 2020-01-25 14:18:07

问题


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

The html form is

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

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?


回答1:


why dont you try something like this..

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...




回答2:


You can use jQuery prepend function:

$("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 + '">');


来源:https://stackoverflow.com/questions/23108323/form-inout-getting-appended-after-the-submit-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!