简体   繁体   中英

Re-set the value of a hidden field in a Coldfusion web page

In a coldfusion CFM page (my_files.cfm) I have a hidden input box with a default value:

<input type="hidden" id="downloadFlag" name="downloadFlag" value="#variables.downloadFlag#">

I also define a CF variable based on the hidden field:

variables.downloadFlag = form.downloadFlag;

On the same page, I define a "Download" button:

<button id="DownloadBtn" class="btn btn-default edit" type="submit" onclick="setDownloadFlag()">Download</button> 

The idea is that the hidden field (downloadFlag) is initially set to 0. When the Download button is clicked, the JS function "setDownloadFlag()" is run, setting the vlaue of the hidden field to 1:

    <script>
    function setDownloadFlag() {
        document.getElementById('downloadFlag').value = 1;
    };
    </script>

Then the page is submitted, and this code executes:

    <cfscript>
    if (variables.downloadFlag eq "1") {
        variables.d_srx = CreateObject('component','my_files');
        variables.d_success  = variables.d_srx.download_files(variables.q_data);
    }
    </cfscript>

The function that is called - download_files() - is in a CFC page (my_files.cfc). This function is defined as:

    <cffunction name="download_files" access="remote" output="yes" returntype="numeric">
        <cfargument name="downloadData" type="query" required="true"> 
    ...[download code here]...
    <cfreturn 0>
    </cffunction>

So far, so good. This actually works, and the download occurs when the button is clicked. The problem is that the hidden field remains set at "1". I need to re-set it to "0".

The problem I am facing is that I don't know how to re-set the value of the hidden field (downloadFlag) back to "0". I'm thinking it should be something like this:

    <cfscript>
    if (variables.downloadFlag eq "1") {
        variables.d_files = CreateObject('component','my_files');
        variables.d_success  = variables.d_files.download_files(variables.q_data);
        document.getElementById('downloadFlag').value = "0";  //Something like this
    }
    </cfscript>

but that doesn't work. Any ideas would be greatly appreciated!

Instead of using a static hidden element, I decided to go with a dynamic element, as in:

function download() {

        $('#form').append('<input type="hidden" id="downloadFlag" name="downloadFlag">').submit();

    };

This way, all I have to do is check for the existence of the element. When I don't need it, I remove it, as in:

document.getElementById('downloadFlag').remove

This seems to work fine. Thank you for the help!

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