如何将值放入jQuery中的数组

时间:2019-07-17 07:18:35

标签: javascript jquery

如何将这些输入值放入数组?当我用.append().push()尝试时,它不会在Chrome控制台中显示值...

$("#anzahlProzesse").change(function () {
    let counter = $('#anzahlProzesse').val();
    for (let i = 1; i <= counter; i++) {
        let row3 = $("<tr><td>" + i + "</td><td>" + "<input type='number' id='prozess" + i + "'></input>" + "</td><tr>");
        $("#sjftable").append(row3);
    }
    $("#berechnen").show();
});    

这应该在点击#berechnen按钮之后完成:

$("#berechnen").click(function () {
    let cpuLaufzeit = [];
    let ankunftszeit = [];
    let anzahlProzesse = $('#anzahlProzesse').val();
    for (let i = 1; i <= anzahlProzesse; i++) {
        let cpu = $("*prozess" + i).val();
        cpuLaufzeit.push(cpu);
    }
    console.log(cpuLaufzeit);
}

2 个答案:

答案 0 :(得分:0)

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, urlJsonObj, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Toast.makeText(MainActivity.this, response.toString(), Toast.LENGTH_SHORT).show();


                try {
                    Toast.makeText(MainActivity.this, "Here", Toast.LENGTH_SHORT).show();
                    Gson gson = new Gson();
                    JSONObject data1 = response.getJSONObject("data");
                    data = gson.fromJson(data1.toString(), Data.class);

                }
                catch (JSONException e){
                    Toast.makeText(MainActivity.this, "Error : "+ e.getMessage(), Toast.LENGTH_SHORT).show();
                }
                dataList.add(data);
                mAdapter = new ViewPagerAdapter(dataList,layoutInflater, viewPager2);

                progressBar.setVisibility(View.GONE);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
                progressBar.setVisibility(View.GONE);
            }
        });
        requestQueue.add(jsonObjectRequest);

答案 1 :(得分:0)

这是该代码的html代码:

<h4>Wieviele Prozesse soll es sein?</h4>
      <div class="input-group">
        <input type="number" id="anzahlProzesse" class="form-control" placeholder="Anzahl an Prozessen"
          aria-describedby="groessen-addon2">
      </div>
    </p>
    <br>
    <br>
    <p>
      <table class="table table-striped" id="sjftable">
        <tr>
          <th>Prozess</th>
          <th>CPU-Laufzeit</th>
        </tr>
      </table>
      <table class="table table-striped" id="fcfstable">
        <tr>
          <th>Prozess</th>
          <th>CPU-Laufzeit</th>
          <th>Ankunftszeit</th>
        </tr>
      </table>

在将值输入到创建的表中之后,应该将这些值放入数组中。