Move this topic
I'm completely stumped.
If I use returned data from a JQ object, my multi-select will not select the current options:
SO THIS DOES NOT WORK:
$("#truck_type").val(data.truck_type);
If I hard code, it works and it's exactly the same as the value of data.truck_type
$("#truck_type").val(["Tandem","Van Trailer"]);
Anyone have any idea?
1
Replies(13)
data.truck_type
should be an array of truck types, even if there is just one (for sanity
’s sake).
JΛ̊KE
Leave a comment on jakecigar's reply
The values are EXACTLY the same:
DOESN'T WORK - DYNAMIC
data.truck_type = ["Tandem","Van Trailer"];
$("#truck_type").val(data.truck_type);
WORKS - HARD CODED
$("#truck_type").val(["Tandem","Van Trailer"]);
See this JSFiddle
. You would have to declare data first.
JΛ̊KE
Leave a comment on MarkM's reply
Jake, thanks for your help.
data.truck_type gets declared via an Ajax call. In fact I can alert(data.truck_type) and I see the values.
Just won't set the multiselect unless I hard code - every other field gets set just fine - :(
$.ajax({
type: "Get",
dataType:
"json",
cache: false,
url:
"process/ajax_data.php",
data: {"id":
$(arguments[0].target).attr("data-id"),"type":5},
success: function(data)
{
$("#loadform").attr("action",
"process/edit_load.php");
$(
"#LoadID" ).val(data.LoadID);
$(
"#LocationID"
).val(data.LocationID);
$('#load_type[value="'+
data.load_type +'"]').prop('checked',
true);
$( "#Hot"
).val(data.Hot);
$( "#Pdate"
).val(data.Pdate);
$( "#Frate"
).val(data.Frate);
$( "#Source"
).val(data.Source);
$( "#City"
).val(data.City);
$( "#State" ).val(data.State);
$("#truck_type").val(data.truck_type);
$( "#Aload"
).val(data.Aload);
$( "#Notes"
).val(data.Notes);
$( "#delete" ).html('
<input name="submit" type="submit"
value="DELETE Load" />');
}
});
Leave a comment on MarkM's reply
I can’t see what your JSON looks like. Is it an array or just some
string with a comma in it?
JΛ̊KE
Leave a comment on jakecigar's reply
I use PHP json_encode and put it into MYSQL.
["Double Drop Deck","End Dump"]
Leave a comment on MarkM's reply
Then it should work. Check your JSON with an online validator like JSONLint - The JSON Validator.
JΛ̊KE
Otherwise I suggest you make a TINY demo that gets a static JSON file
and loads the select.
Leave a comment on jakecigar's reply
I use PHP json_encode and put it into MYSQL.
jQuery
doesn't care about either. It cares what it gets.
["Double Drop
Deck","End Dump"]
If
that's what it gets, it's valid JSON. But your code
doesn't treat it as what it is - an array. Your code seems to
expect an object.
$( "#LoadID" ).val(data.LoadID);
$( "#LocationID" ).val(data.LocationID);
$( "#LocationID" ).val(data.LocationID);
And
your data doesn't look anything like what your code expects.
Given
that code, I'd expect the JSON to look something like:
{"LoadID":"12345","LocationID":"67890"
... }
Not sure what you mean by that. You mean you stored the encoded JSON in your database? Why? It's really not a very useful way to store stuff in a database, but if you wish...and put it into MYSQL
And you would be wrong. Look up JSON and use your colons.
JΛ̊KE
Leave a comment on watusiware's reply
The problem hinges on what "it" is...I use PHP json_encode and put it into MYSQL
Which
points out why it is next to useless to post descriptions and little
snippets showing parts of what you did. Post exactly what you
did. Post a link to your site, or make a demo using jsFiddle and
Plunkr, and then we will know exactly what you did.
I'd
guess (and that's all I can do is guess) that you've
double-encoded part of your data.
That is, you stuff some JSON
into your database (an odd thing to do, but whatever...) and
then you retrieved a row from your database and JSON-encoded the
row. So, now you have some column in that row that is itself JSON-encoded.
Leave a comment on watusiware's reply
And you would be wrong. Look up JSON and use your colons
Fixed.
Given that code, I'd expect the JSON to look something like:{"LoadID":"12345","LocationID":"67890" ... }
Leave a comment on watusiware's reply
The important part is that the portion of the JSON looks like
- "truck_type": ["Double Drop Deck","End Dump"],
Not
- "truck_type": "Double Drop Deck,End Dump",
or
anything else.
JΛ̊KE
Leave a comment on jakecigar's reply
Change topic type
Link this topic
Provide the permalink of a topic that is related to this topic
Reply to MarkM's question
{"z57915333":[14737000006433635,14737000006435405,14737000006435407,14737000006435409],"z2950240":[14737000006430955,14737000006436195,14737000006436197,14737000006436199,14737000006436201,14737000006436203,14737000006436211],"z7664639":[14737000006434178,14737000006434180,14737000006434194]}
Statistics
- 13 Replies
- 269 Views
- 1 Followers


