javascript - Cannot read property 'done' of undefined due to switch -
i have function gaining data ajax. problem construction switch causing 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>
the cause of error getting value of type
not match either of case
statements. thus, out
remains undefined
, gives error see.
in addition, must use break;
statements in case of case:
statements , {competition.value}
not valid es5 javascript. perhaps want 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 meant {competition.value}
. guessed maybe wanted {sport: competition.value}
, don't see competition
defined anywhere i'm not sure.
or, perhaps remove duplicate code , 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); }); } }
Comments
Post a Comment