Sunday, 15 June 2014

javascript - Cannot read property 'done' of undefined due to switch -


I have a function that is receiving data by AJAX The problem is that the construction switch is caused by this error: <

Div class = "snippet" data-lang = "js" data-hide = "wrong">

  & lt; Script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js" & gt; Activate the function changes (input, type) {var out; Switch (type) {case "game": out = $ Post ("/ ajax / ajax.php", {sport: input.value}); Case "Competition": Outside = $ Post ("/ ajax / ajax.php", {competition.value}); } Out.done (function (data) {$ ("# output") .html (data);}); } & Lt; / Script & gt; Thanks for the answer  

The possible reason for the error that you receive is because of the type The value does not match the match of your case statement.

In addition to this, you must break; Statement should be used in your case case: statement and {competition.value} valid ES5 Javascript is not something you want to do something like this: / P>

  Enable function changes (input, type) {var out; Switch (type) {case "game": out = $ Post ("/ ajax / ajax.php", {sport: input.value}); break; Case "Competition": Outside = $ Post ("/ ajax / ajax.php", {sport: competition.value}); break; Default: Break; } If (outside) {out.done (function (data) {$ ("# output"). Html (data);}); }}  

I do not know what you mean by your {competition.value} . I found that maybe you wanted to get it done {sport: competition.value} , but I do not know that competition is defined anywhere, so I'm not really sure. Or, maybe remove some duplicate code and use it:

  Activate the function changes (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);}); }}  

No comments:

Post a Comment