Reputation: 20
I have a function which gaining data by ajax. The problem is that construction switch causing this error:
cannot read property 'done' of undefined
I don't know why...
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
function changeSelect(input,type)
{
var out;
switch(type)
{
case "sport" : out=$.post("/ajax/ajax.php",{sport:input.value});
case "competition" :out=$.post("/ajax/ajax.php",{competition.value});
}
out.done(function(data)
{
$("#output").html(data);
});
}</script>
Upvotes: 0
Views: 3773
Reputation: 707456
The likely cause of the error you are getting is that the value of type
does not match either of your case
statements. Thus, out
remains undefined
and gives you the error you see.
In addition, you must use break;
statements in case of your case:
statements and {competition.value}
is not valid ES5 Javascript. Perhaps you want something like this:
function changeSelect(input, type) {
var out;
switch (type) {
case "sport":
out = $.post("/ajax/ajax.php", {sport: input.value});
break;
case "competition":
out = $.post("/ajax/ajax.php", {sport: competition.value});
break;
default:
break;
}
if (out) {
out.done(function(data) {
$("#output").html(data);
});
}
}
I don't know exactly what you meant with your {competition.value}
. I guessed that maybe you wanted it to be {sport: competition.value}
, but I don't see competition
defined anywhere so I'm not really sure.
Or, perhaps remove some duplicate code and use this:
function changeSelect(input, type) {
var val;
switch (type) {
case "sport":
val = input.value;
break;
case "competition":
val = competition.value;
break;
default:
break;
}
if (val) {
$.post("/ajax/ajax.php", {sport: val}).then(function(data) {
$("#output").html(data);
});
}
}
Upvotes: 3