admin管理员组

文章数量:1393900

<html>
<body>
    <script language="javascript">
    document.getElementById('myfileId').onchange = function(e) { alert('change'); }
    </script>

    <form action="" >
        <input type="file"  id="myfileId" name="myfile">
    </form>

    </body>
</html>

How can I call the JavaScript function after a file selection.

Edit: 1

<html>
<body>

    <form>
        <input type="file"  id="myfileId" name="myfile">
    </form>
 <script language="javascript">
  window.onload = function() 
        { 
    alert("Test");

    document.getElementById('myfileId').onblur =  function(e) { alert('change'); }
        }
    </script>
    </body>
</html>
<html>
<body>
    <script language="javascript">
    document.getElementById('myfileId').onchange = function(e) { alert('change'); }
    </script>

    <form action="" >
        <input type="file"  id="myfileId" name="myfile">
    </form>

    </body>
</html>

How can I call the JavaScript function after a file selection.

Edit: 1

<html>
<body>

    <form>
        <input type="file"  id="myfileId" name="myfile">
    </form>
 <script language="javascript">
  window.onload = function() 
        { 
    alert("Test");

    document.getElementById('myfileId').onblur =  function(e) { alert('change'); }
        }
    </script>
    </body>
</html>
Share Improve this question edited May 10, 2010 at 19:55 coderex asked May 10, 2010 at 19:20 coderexcoderex 27.9k46 gold badges119 silver badges172 bronze badges 3
  • Why there is no <html> element! In fact, I don't even know what language this is. </sarcasm> Could you add a bit of context to your question such as what error message and behavior(expected and actual) and such? – Earlz Commented May 10, 2010 at 19:21
  • For your edit, I didn't actually mean to make a full HTML document. I just meant you needed more context such as "what error message and behavior(expected and actual)" – Earlz Commented May 10, 2010 at 19:47
  • @Earlz I need to call a javascript function after the selection of file from the Open Dialog window close event. – coderex Commented May 10, 2010 at 19:51
Add a ment  | 

2 Answers 2

Reset to default 8

You have to put that <script> after the <input>. The document.getElementById('myfileId') does not exist if you put it before, so it will return nothing.

As @Kenny says, you have to either put the script after the input declaration so the element actually exists when the script is executed, or alternatively, add the function to the document's onload event:

window.onload = function() 
        { document.getElementById('myfileId').onchange =  
          function(e) { alert('change'); }
        }

本文标签: