unable to set the html content in a custom jQuery function -


I have a jQuery function called "bindServingSize", which I call it

  $ (Servingsizes) .bindServingSizes (ingredientid);  

Where "Servings" is a valid drop-down list control.

  jQuery.fn.bindServingSizes = function (ingredientid) {$ .getJSON ("/ serving_sizes? Ingredientid =" + ingredientid, function (j) {var option = "& lt; option value = & # 39; & gt; Please choose a serving size & lt; / option & gt; "; (var i = 0; i & lt; j.length; i ++) option + = '& lt; option value =' ' + J [i] .serving_size.id + '"& gt;' + j [i]. Conservation_ shape.name + '& lt; / option & gt;' $ $ (this) .html (option);}); };  

I get data from the server and I see it displaying it on the page in the "Options" variable. However, I am not able to set html content in element. What am I missing?

Thanks

The problem is that $ (this) is $ .getJSON The selection box is no longer in the callback function, it creates a variable that stores the reference box of the selection box.

  jQuery.fn.bindServingSizes = function (ingredientid) {var $ this = $ (this); $ .getJSON ("/ serving_sizes? Ingredientid =" + ingredientid, function (j) {var option = "& lt; option value = '> & gt; please select a serving size & lt; / option & gt;"; ( Var i = 0; I & lt; j.length; i ++) option + = '& lt; option value =' '+ [j] [i] .serving_size.id +' "'gt;' + [ I] Protection_Size Name + '& lt; / option & gt;' $ $ (optional);}); };  

Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

jquery - SimpleModal Confirm fails to submit form -

php - Multiple Select with Explode: only returns the word "Array" -