Reputation: 430
I'm trying to load some data from my Json file with jquery, but I'm not sure, why it wont work! I'm also trying to sort and find a specific object in the array in the Json file. Here is some of my code :
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script type="text/javascript">
var url = 'content.json';
var outp = {
low : 0,
high : 99,
name : "Fritz",
rufnummer : "012",
faxnummer : "345",
mobil : "678",
mail : "[email protected]",
}
var searchplz = parseInt(.inp.toString().substr(0,2));
$(.find).click(function(){
$.getJSON(url,data,function(data,status)){
if(status === 200 && .inp.length == 5){
if(searchplz != 85){
for (var i = data.PLZ.length - 1; i >= 0; i--) {
if(data.PLZ[i].low <= searchplz && data.PLZ[i].high >= searchplz){
outp = data.PLZ[i];
}
}
} else {
searchplz = parseInt(.inp.toString().substr(0,3));
if (searchplz == 850 || searchplz == 851){
outp = data.PLZ[21];
} else {
outp = data.PLZ[22];
}
}
}
}
});
</script>
</head>
<body>
<p>Postleitszahl:</p>
<input type="number" autocomplete="on" name="inp" class="inp">
<button type="button" class="find">Finden</button>
<p class="output"></p>
</body>
Upvotes: 2
Views: 170
Reputation: 1758
If it's not a typo, your click event certainly dont trigger:
$(.find).click
should be
$('.find').click
Also, the data
parameter doesn't exists (at least in the code you show us)
$.getJSON(url,data,
EDIT :
There is another problem in your code. You use .inp
as a variable, when it's only a CSS class.
You can't do
var searchplz = parseInt(.inp.toString().substr(0,2));
The .inp.toString()
part is wrong. You need to use jquery to get the DOM element with the class .inp
, then you'll be able to use its value :
var inp = $(".inp").val();
Here you get the input with class .inp
, and assign its value to the variable inp
. inp
is now a string.
You can now use the var inp
like this :
var searchplz = parseInt(inp.substr(0,2));
Report this modification in your whole code.
Upvotes: 3