How to clear text field value on history.back()

2019-07-25 00:08发布

问题:

I have a ColdFusion condition like this:

<cfif txtTaxFileNo neq "">
    <script>
        alert("NPWP Already Exist");
        history.back();
    </script>
    <cfabort>   
</cfif>

Assume txtTaxFileNo has a value of "123" in the previous page. How can I empty the txtTaxFileNo field? I already tried this:

<cfif txtTaxFileNo neq "">
    <script>
        alert("#JSStringFormat('NPWP Already Exist')#");
        history.back();
        txtTaxFileNo.value = "";
    </script>
    <cfabort>   
</cfif>

However, the textfield on the previous page is not empty. It still has a value of "123". Thanks in advance.

回答1:

Don't usehistory.back() because that restores form state. If you want to load a fresh page, just load a fresh page.

<cfif txtTaxFileNo neq "">
    <script>
        alert("NPWP Already Exist");
        window.location = "form URL here";
        // or, if the URL is the same
        window.location.reload(true);
    </script>
    <cfabort>   
</cfif>

See window.location.reload() docs on MDN.