Event.observe()无效; ProtoytypeJS库

时间:2012-08-22 03:26:03

标签: javascript html prototypejs

我有这段代码。

<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>

这不起作用。我无法得到任何警报。有人可以建议一个解决方案吗?

1 个答案:

答案 0 :(得分: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

或者,如果您想将脚本标记保留在 head 中,则其他选项为:

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