如何使用JQUERY访问数组的元素?

时间:2021-12-14 08:10:11

I'm new in JQUERY and trying AJAX.I need to access the array so i can put the element on my html table. btw the array comes from my backend.

我是JQUERY新手,尝试AJAX。我需要访问数组,以便将元素放在html表中。这个数组来自我的后端。

the elements in the data array are:

数据数组中的元素为:

[1990, "098765", "094561", "098123", "097612"]

I tried the usual way but it wont work, How to do it right? really need help

我用了惯常的方法,但行不通,怎么做才对呢?真正需要帮助

my Jquery code:

我的Jquery代码:

 $('#trigger').click(function(){
            var send = $('#myselect').val();
            $.ajax({

                data:{
                  sent: send
                },
                type: 'POST',
                url: '/delinquincy'
            })
            .done(function(data){
              console.log(data)
              var q1=data[1]
              var q2=data[2]
              var q3=data[3]
              var q4=data[4]

              $('#q1').html(q1)
              $('#q2').html(q2)
              $('#q3').html(q3)
              $('#q4').html(q4)
            })
          })

This is where I put the elements of the array,

这是我放置数组元素的地方,

my HTML Table:

我的HTML表:

<table class="table table-bordered" id="subtable">
              <thead>
                  <tr>
                    <th scope="col">Quarter</th>
                    <th scope="col">O.R.number</th>
                    <th scope="col">Action</th>
                  </tr>
              </thead>
              <tbody>
                <tr>
                  <td>1st Quarter</td>
                    <td id="q1"> </td>
                    <td><button type="button" class="btn btn-primary subbutton" data-toggle="modal" data-target="#submodal" id="pay" data-row-val="" >Pay</button></td>
                </tr>

                <tr>
                  <td>2nd Quarter</td>
                    <td id="q2"> </td>
                    <td><button type="button" class="btn btn-primary subbutton" data-toggle="modal" data-target="#submodal" id="pay" data-row-val="" >Pay</button></td>
                </tr>

                <tr>
                  <td>3rd Quarter</td>
                    <td id="q3"> </td>
                    <td><button type="button" class="btn btn-primary subbutton" data-toggle="modal" data-target="#submodal" id="pay" data-row-val="" >Pay</button></td>
                </tr>

                <tr>
                  <td>4th Quarter</td>
                    <td id="q4"> </td>
                    <td><button type="button" class="btn btn-primary subbutton" data-toggle="modal" data-target="#submodal" id="pay" data-row-val="" >Pay</button></td>
                </tr>

              </tbody>
            </table>

1 个解决方案

#1


3  

Since you have no dataType set on the request, the response will always come as a string, not as a ready to use javascript array. Use

由于在请求上没有设置数据类型,因此响应总是以字符串形式出现,而不是准备使用javascript数组。使用

data = JSON.parse(data) 

before

之前

$('#q1').html( data[0] )

or add

或添加

dataType: "json",

to your AJAX request.

你的AJAX请求。

#1


3  

Since you have no dataType set on the request, the response will always come as a string, not as a ready to use javascript array. Use

由于在请求上没有设置数据类型,因此响应总是以字符串形式出现,而不是准备使用javascript数组。使用

data = JSON.parse(data) 

before

之前

$('#q1').html( data[0] )

or add

或添加

dataType: "json",

to your AJAX request.

你的AJAX请求。