简体   繁体   中英

How do I append a string to form results using javascript?

I've got a form which posts its results using "get" method. I was wondering how I could add some additional values to these results before they are submitted.

What I did so far is overload the submit function so I could concat my additional string which I wish to pass along with the form results to the destination url.

On regular submit the destination url gets the results as follows:

   ..dest url?txtName=e&txtAge=r&gender=mail&select=1

What I am attempting to do is append another string to these results

  ..dest url?txtName=e&txtAge=r&gender=mail&select=1|myStringResult

I've overloaded the submit

    function overLoadSubmit() 
    {
        window.addEventListener('submit', newSubmit, true);
        HTMLFormElement.prototype._submit = HTMLFormElement.prototype.submit;
        HTMLFormElement.prototype.submit = newSubmit;
    }

The original submit is called after newSubmit() , and that's where I would like to append my string.

    function newSubmit()
    {           
        var myForm = document.forms[0];
        var s = "|myStringResult";             
        // here is where i would like to append s ,
        // but i can't figure out how to reference the form results  
        // or how to add it to the original submit
    }

I've also tried adding an hidden input control

    <input type="hidden" id="_body" value="getValues();"/>

since myResultString is really long and is in fact an entire html document

    function getValues() 
    {
        var _doc = document.childNodes[1].innerHTML;
        return '|' + _doc; 
    }

This doesn't seem to work.

Why not add a hidden field to the form instead?

<input type="hidden" name="myhiddenfield" value="myStringResult">

The value will get appended to the URL when the form is submitted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM