简体   繁体   中英

Return value not display and how to pass value received using input button

When I return the value 'filename', and try to display the that value using document.getElementById("getfilename").innerHTML = filename, the value not display. I tried to use macro recording to check the value 'filename' hold, and yes, its hold the value I want. but not display when I try to print. Is any wrong in my code?

html code:

<form>
    <input type='file' id="poem" accept='text/plain' onchange='printPoem(event)' >
</form>

<script>
    if (filename !== null){
        document.getElementById("getfilename").innerHTML = filename;
        //document.write(filename);
     }
</script>

js code:

function printPoem(event) {
    var input = event.target;
    var reader = new FileReader();
    reader.onload = function(){
        var text = reader.result;
        //document.write(reader.result.substring(0, 200));
        var fullPath = document.getElementById("poem").value;

        if (fullPath) {
            var startIndex = (fullPath.indexOf('\\') >= 0 ? 
            fullPath.lastIndexOf('\\') : fullPath.lastIndexOf('/'));
            filename = fullPath.substring(startIndex);
            if (filename.indexOf('\\') === 0 || filename.indexOf('/') === 0{
                filename = filename.substring(1);
            }
                //alert(filename);
        }
        document.getElementById("displayPoem").innerHTML = text;
    };
    reader.readAsText(input.files[0]);              
    return(filename);
}

Second question, how I pass the value (filename) that I tried to display early using these input button?

<input type="button"  class="button" value="Visualize" onclick="window.location.href='visualization.html'">
  1. Html tags will render from top to buttom, when render a <script> tag, the javascript in it will execute. I think your question is that the change event has not triggered, there is no filename variable in your program. Put these code into the printPoem function is ok.
  2. I think you can set a global variable, and the event will change this global variable.

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