Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

javascript - How to use jQuery forEach statement?

Note please. (I'm currently using the Spring Framework (MVC))

The value sent from Controller to ajax is ...

enter image description here

It looks like the picture above.

I am looping through the forEach statement for the values in that array, and I want to put the value into the tag.

So I wrote the code.

$(function()
    {
        $('#doctorSelect').change(function()
        {
            $('#selectGugan').show();

            var doctor_idx = $(this).val();
            $.ajax
            ({
                type: 'POST',
                url: 'selectDoctor.do',
                data: {"d_idx":doctor_idx},
                dataType: 'JSON',
                success: function(sectionDate)
                {
                    console.log(sectionDate);
                    var html = "<option value=''>Choice Doctor</option>";
                    var responseData = [];
                    responseData.push(sectionDate);
                    console.log(responseData);

                    $.each(responseData, function(key, value)
                    {
                        console.log("forEach statement in responseData :" + responseData);

                        //html+="<option value="+new_date+">"+new_date+"</option>"; 
                    });
                    $('#doctorSelect_2').html(html);
                },
                error: function(sectionDate)
                {
                    console.log("data : " + sectionDate);
                }
            });
        });
    });

But unexpectedly, I do not get the key, value.

In fact, I don t know about the jquery forEach statement.

How do I set key, value?

I just want to bring those values and express it like this.

<option value="ri_idx">ri_startDate ~ ri_endDate</option>

How to set key, value or How to use jquery forEach statement ?

I am a beginner. I would appreciate it if you could give me an example.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In your case I am not sure why would you be doing this:

responseData.push(sectionData);

because this way you dont get an array of objects as I believe you thought you would, you simply will get an array with 1 element in it, which is many objects, so doing a forEach on that array will not help, because the value will be the multiobject element (not a single object that you could access properties).

What you want to do is iterate over your original objects, so your code should be something like this:

$.each(sectionDate, function(key, value){
    // here you can access all the properties just by typing either value.propertyName or value["propertyName"]
    // example: value.ri_idx; value.ri_startDate; value.ri_endDate;
});

Hope this helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...