i have a textbox. when the user tabs out of it (.change) - i want to run a json query to get a single string and populate a textbox with it.
everything works fine except the fill on the callback. every single demo/bit of code i can find deals strictly with populating a selectlist on the callback. i do not want to do this.
$("#CodeID").change(function() {
var codeID = $("#CodeID").val();
var url = '/diagnosisCodes/GetCodeDescription?CodeID=' + codeID;
$.getJSON(url, null, function(data) {
$("#txtDescription").val(data);
});
});
that's what i ha开发者_运维知识库ve now. obviously does not work. any ideas?
The issue I see looking this over is that the data
parameter in your success function will be a JSON object. So, for example, if your ajax call is returning JSON that looks like
{description:"Here's my description"}
you'll want to change the line that loads the description to
$("#txtDescription").val(data.description);
It's probably because data
is a json data (javascript object) you must get the field that you want from the object
Instead of $("#divDescription").val(data);
Have you tried $("#divDescription").append(data);
or $("#divDescription").html(data);
?
edit
After a brief test I found that val or append will both work if it's a textarea and not a div you are adding to. http://jsbin.com/usije5. Val will just wipe any existing data while append will append to existing data.
MVC Controller had started throwing 500s, because i didn't have JsonRequestBehavior.AllowGet set.
Jquery Code is good. thanks for the help!
精彩评论