2012-08-22 56 views
0

我有這段代碼。Event.observe()不起作用; ProtoytypeJS庫

<html> 
    <head> 
     <script src="//ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/prototype.js"> </script> 
     <script> 
      Event.observe('clickme', 'click', function(event) { 
       alert('clicked'); 
      }); 

      Event.observe('uploadme', 'change', function(event) { 
       alert('hola'); 
      }); 

     </script> 
    </head> 
    <body> 
     <form enctype="multipart/form-data" action="insertimage.php" method="post" name="changer"> 
      <input name="MAX_FILE_SIZE" value="10240000" type="hidden"> 
      <input id="uploadme" type="file"> 
      <input type="button" id="clickme" value="Upload Stuff!" /> 
      <input id="sub" type="submit" /> 
     </form> 
    </body> 
</html> 

這是行不通的。我無法對任何事情保持警惕。任何人都可以請建議解決方案?

回答

0

嘗試添加腳本略高於關閉標籤,如:

</head> 
<body> 
<form enctype="multipart/form-data" action="insertimage.php" method="post" name="changer"> 
<input name="MAX_FILE_SIZE" value="10240000" type="hidden"> 
<input id="uploadme" type="file"> 
<input type="button" id="clickme" value="Upload Stuff!" /> 
<input id="sub" type="submit" /> 
</form> 
<script> 
Event.observe('clickme', 'click', function(event) { 
    alert('clicked'); 
}); 
Event.observe('uploadme', 'change', function(event) { 
    alert('hola'); 
}); 
</script> 
</body> 

演示:jsFiddle

或者,如果你想保持腳本標籤中 ,其他選項將是:

Event.observe(window, 'load', function() { 
    Event.observe('clickme', 'click', function(event) { 
     alert('clicked'); 
    }); 
    Event.observe('uploadme', 'change', function(event) { 
     alert('hola'); 
    }); 
}); 
+0

謝謝它的工作! :) –