【问题标题】:how to insert array values into specific td id如何将数组值插入特定的 td id
【发布时间】:2017-07-02 18:42:20
【问题描述】:
<table id="tbl" class="tbl1">
  <tr>
    <th>
      mobileno
    </th>
    <td>
    </td>
  </tr>

</table>

我可以通过使用 for 循环来获取特定值,但我无法将这些特定值附加到特定的 td id 中。我已经将 td 的 id 设置到 html 表中。但我只能在第一行附加并且然后对于第二行,它不会将那些进一步的特定值插入下一行。 如何将数组值插入特定的 td id

【问题讨论】:

  • 你试过 $("#mo_"+i) 而不是 $("#mo_1")??
  • folio() 用于定义它的地方是什么?
  • 您是否特别喜欢您编写 javascript 的方式?我可以想出一些更简单的方法来解决这个问题,在此过程中提供更多动态功能。
  • var mo_id = $("#tbl td").attr('id');
  • 上面写的方式我要获取td的id

标签: javascript jquery html arrays ajax


【解决方案1】:

试试这个而不是你的数组:

  var intArr = [111, 201, 345, 434, 532, 677, 790, 890, 989, 118, 107, 136, 125, 153, 142];

$("#mo_" + i).append(arrfo);

【讨论】:

    【解决方案2】:

    使用

    $("#mo_" + i).append(intmo[j]);
    

    【讨论】:

      【解决方案3】:

      试试这个

      var arrfo = [];
      $(document).ready(function() {
        var intArr = [111, 201, 345, 434, 532, 677, 790, 890, 989, 118, 107, 136, 125, 153, 142 ];
        populatevalues(intArr);
      });
      
      
      function populatevalues(intArr) {
        for (i = 0; i < 6; i++) {
          for (j = i * 3; j < 3; j++) {
            var arrfo = intArr[j];
            $("#mo_"+j).append(arrfo);
          }
        }
      }
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
      <table id="tbl" class="tbl1">
        <tr>
          <th>
            mobileno
          </th>
          <td id="mo_0"> 
          </td>
        </tr>
        <tr>
          <th>
            mobileno
          </th>
          <td id="mo_1">
          </td>
        </tr>
        <tr>
          <th>
            mobileno
          </th>
          <td id="mo_2">
          </td>
        </tr>
        <tr>
          <th>
            mobileno
          </th>
          <td id="mo_3">
          </td>
        </tr>
        <tr>
          <th>
            mobileno
          </th>
          <td id="mo_4">
          </td>
        </tr>
        <tr>
          <th>
            mobileno
          </th>
          <td id="mo_5">
          </td>
        </tr>
      </table>

      【讨论】:

      • 这样它在使用#mo+j的地方不起作用。这只插入第一行,我的html页面是我hvg代码的方式。不要将第一个单元格设为mo_0
      • var mo_id = $("#tbl td").attr('id');$("#mo_"+i).append(a);我要这样
      • 根据你的循环,只有 i 可以是 0。
      • 我只使用 for 循环解决了我的问题。感谢大家提出你自己的想法
      【解决方案4】:

      更新:现在这与您的代码完全不同,但是它将允许您将 strm 数组中的项目动态插入 &lt;td&gt;&lt;/td&gt; 但 td 必须具有类 mo - 就像 &lt;td class="mo" id="mo_1"&gt;&lt;/td&gt; 您可以保留您拥有的 id。它们不会干扰代码。

      解释:我使用 jQuery.each() "$.each(function(){})" 函数遍历每个具有 'class="mo"' 的 html 元素,随着函数的进行插入相应的 strm 数组项,直到有对象列表中不再有 mo。这将允许您添加任意数量的带有类 mo 和 strm 项目的元素,而无需更改任何 jquery 代码。

        $(document).ready(function() { // encapsulate all functions in document ready function
      
          // this is the strm array, in order to have a proper array you must have values inside "value" separated by ,
          var strm = ["111", "201", "345", "434", "532", "677", "790", "890", "989", "118", "107", "136", "125", "153", "142"];
          // execute mo, pass strm array to function.
          mo(strm);
      
          // mo function with array as input value
          function mo(array) {
            // jQuery.each() method.
            // pass array as input value.
            $.each(array, function(i, val) { // << index and value of each item in strm array
              // if i is less than or equal to the number of elements with class mo,
              if (i <= $(".mo").length) {
                // get element with class mo equal to strm array index, insert value
                $(".mo").get(i).append(val);
              }
            });
          }
        });
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <table id="tbl" class="tbl1">
        <tr>
          <th>
            mobileno
          </th>
          <td class='mo' id="mo_0">
          </td>
        </tr>
        <tr>
          <th>
            mobileno
          </th>
          <td class='mo' id="mo_1">
          </td>
        </tr>
        <tr>
          <th>
            mobileno
          </th>
          <td class='mo' id="mo_2">
          </td>
        </tr>
        <tr>
          <th>
            mobileno
          </th>
          <td class='mo' id="mo_3">
          </td>
        </tr>
        <tr>
          <th>
            mobileno
          </th>
          <td class='mo' id="mo_4">
          </td>
        </tr>
        <tr>
          <th>
            mobileno
          </th>
          <td class='mo' id="mo_5">
          </td>
        </tr>
      </table>

      【讨论】:

      • 实际上如果我的静态数组有超过 15 个值,所以我想将超过 15 个值附加到同一个 html 表的动态行中。这就是为什么我在这里使用 for 循环来插入数组值在 html 表中具有小于或等于 15 个值。其中每行在 html 表的每一行中可以有最小 1 个值和最大 3 个值
      • 好的,我已经更新了我的答案,以使用 jQuery 提供一个非常好的动态插入功能。我希望它符合您的需求,并且您可以从中学到一些东西! :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-23
      • 2014-07-22
      • 2021-02-27
      • 2017-04-20
      • 1970-01-01
      相关资源
      最近更新 更多