-1

I am having json as below

[{
    "name": {
        "1": "User Name",
        "2": "User Email",
        "3": "User Mobile",
        "4": "User Address"
    },
    "filename": [
        ["upl_1407158917.xls"]
    ]
}]

I want to apend it to drop down list in ajax and filename want to store in hidden field.

I don't want to append filename in my drop down list.

can anyone help me out.

Updated :

I tried this :

$.each(data, function(key, value) { 
//alert(value); 
$('.excelHead').append( $('<option value="' + value + '">' + value + '</option>') );
 });
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
Anjali
  • 43
  • 5

2 Answers2

0

You need to run EACH loop for adding it to drop down.for example :

 select_box = '<select>';
    $.each(data.name, function(k, v) {
      withdraw_option +='<option value="'+k+'">'+v+'</option>';
    });
    select_box += '</select>';

Will produce :

<select>
<option value="1">value1</option>
<option value="2">value2</option>
<option value="3">value3</option>
<option value="4">value4</option>
</select>
Harshal
  • 3,562
  • 9
  • 36
  • 65
0

You gave no specifics as to what you want appended where, so here's just some simple code that shows how to get each item in your array, and append the data. Here's what you need to do:

in a jQuery each, parse the data, then pass it into the callback. It then becomes as simple as you'd expect.

$.each(data, function(index, value) {
    var option = $("<option>")
        .text(value.name["1"])
        .val(value.name["2"]);
    $("#mySelect").append(option);
});

I created an option (as per your request), and used some of the data to fill in the text and value for it. The same exact way can then be used to create a hidden field for the file.

http://jsfiddle.net/7cp2j/1/

ndugger
  • 7,373
  • 5
  • 31
  • 42
  • Just a quick question, why are you including the Email as a value to the combo, when he haven't mentioned that, and why are you using numbers as indexes like this? He want's the file name instead – ziGi Aug 04 '14 at 14:44
  • He gave no specific instructions on what to put where, so I just gave an example. Also, look at his data above to see why I used numbers. – ndugger Aug 04 '14 at 14:45
  • I know why you used numbers but I think it would be much more readable if you are able to give some meanings to those numbers because I had to go up and take a look at what's the data actually in there. What happens if in a moment 2. is not email but something else? Additionally replacing strings with constants/enums is much preferable. – ziGi Aug 04 '14 at 14:47
  • I was simply going off of the data that he gave me. i'm not going to completely change the layout of his objects -- that wouldn't be nearly as helpful. – ndugger Aug 04 '14 at 14:48
  • 1
    Yes but I believe it promotes good coding practices so you could also take a look into that, it might help you not edit 1000 files in the future. – ziGi Aug 04 '14 at 14:49
  • ... Zigi, it's not my data, if you want to tell someone about how it's not good to have numbers as your keys in an object, feel free to tell the OP. Don't shoot the messenger, here. – ndugger Aug 04 '14 at 14:50