最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use jQuery forEach statement? - Stack Overflow

programmeradmin3浏览0评论

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

The value sent from Controller to ajax is ...

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.

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

The value sent from Controller to ajax is ...

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.

Share Improve this question edited Dec 15, 2017 at 9:25 Nitin Dhomse 2,6221 gold badge14 silver badges26 bronze badges asked Dec 15, 2017 at 9:21 user8057627user8057627 8
  • 1 console.log("forEach statement in responseData :" + responseData); replace this with console.log(key); console.log(value); and you will get individual key/val – N. Ivanov Commented Dec 15, 2017 at 9:24
  • OK, I will fix it. – user8057627 Commented Dec 15, 2017 at 9:25
  • 0 / [object Object] (key / value) – user8057627 Commented Dec 15, 2017 at 9:26
  • 1 well now to get the individual properties of value, you can simply do value.propertyName, where propertyName is whatever is in your response. – N. Ivanov Commented Dec 15, 2017 at 9:27
  • 1 oh why would you do responseData.push(sectionDate)? You can simply do $.each(sectionDate, function(key, value){ ... }); and then it should work. Hope this helps – N. Ivanov Commented Dec 15, 2017 at 9:31
 |  Show 3 more ments

3 Answers 3

Reset to default 4

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!

In jQuery forEach working like this

$.each( obj, function( key, value ) {
  alert( key + ": " + value );
});

If you are using Array for loop than key is array index and value values and if you are using javascript object then object key is key and value is object value for the particular key.

You can read more at jQuery documentation.

Just use property names in object. Check if this helps

$(document).ready(function() {
  var responseData = [
    {startdate: '2017-12-21', enddate: '2017-12-22', idx: '1'},
    {startdate: '2017-11-21', enddate: '2017-11-22', idx: '1'},
    {startdate: '2017-10-21', enddate: '2017-10-22', idx: '1'}
  ];
  var html = '';
  $.each(responseData, function(key, value) {
    html += "<option value=" + value.startdate + ">" + value.startdate + "</option>";
  });
  $('#doctorSelect').html(html);
});
发布评论

评论列表(0)

  1. 暂无评论