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
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
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)
});
});
精彩评论