开发者

Can't load a simple function

Here's my code

<开发者_开发知识库script type="text/javascript" href="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript" href="js/js.js"></script>
...
<input type="text" name="city" onkeyup="loadStation(this.value);"/>
...

and the js.js file

//Load Stations
function loadStation(stationCity){
  alert(stationCity);
}

When I try to trigger the loadStation onkeyup, nothing's happening. The path of js/js.js is ok since in the source I can load it.

What could be wrong ?

EDIT

I fell really stupid.... instead of href="" it should have been src="". The funiest thing is that even you guys haven't notice it :P

Thanks


You could be wrong about `js/js.js` I'd suggest using Firebug and checking that you're not getting a 404 error from your js file. You may have wanted to use a root-relative link to the js file: `/js/js.js` so that you don't have issues with sub-directories.

I've often professed the beauty of HTML CSS & JavaScript as an MVC pattern. So I wont go into that here, however. You should keep your HTML in .html files, your CSS in .css files, and your JavaScript in .js files

Using inline javascript event attributes breaks the nice MVC pattern that I often describe, so it's much better to attach the events in the script itself:

//wrap this in a document.ready or window.onload event callback
//-or-
//place the script *after* the input element has been added to the dom
var input;
//replace with whatever selector 
input = document.getElementsByTagName('input')[0];works
input.onkeyup = function(){ loadStation(this.value) };

...more code...

function loadStation(val)
{
  alert(val);
}

Fiddle

A couple more debugging questions: * Are you testing a flat html & js file locally? * Are you uploading flat files to a server? * Are you able to post a url of where the content is located?

You're using href instead of src on the script element.


try to add the onkeyup like this in your js file:

$('body').ready(function(){
     $('#idOfInput').onkeyup(function(){
         loadStation(this.value)
     });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜