将动态创建的文本框的值按顺序存储在 JavaScript 数组中。

huangapple go评论73阅读模式
英文:

Store dynamically created text box values in an javascript array in an order

问题

I have developed dynamically created text boxes by clicking a button using AJAX. I have three text box IDs such as morning, noon, and night. I just want to store these values in an array like [morning, noon, night][morning, noon, night]....

I have tried, but it stored like [morning, morning, noon, noon, night, night]. I can't understand how to separate as I mentioned above.

Please help me to solve my problem.

英文:

I have developed dynamically created text boxes by clicking a button using AJAX. I have three text box IDs such as morning, noon and night. I just want to store these values in an array like [morning, noon, night][morning, noon, night]...

I have tried, but it stored like [morning, morning, noon, noon, night, night]. I can't understand how to separate as I mentioned above.

Please help me to solve my problem.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(document).ready(function() {
  $(document).on(&#39;click&#39;, &#39;#submit&#39;, function() {
    var modess = [
      $(&quot;input[id=&#39;morning[]&#39;]&quot;).map(function() {
        return $(this).val();
        console.log(morning);
      }).get(),

      $(&quot;input[id=&#39;noon[]&#39;]&quot;).map(function() {
        return $(this).val();
        console.log(noon);
      }).get(),

      $(&quot;input[id=&#39;night[]&#39;]&quot;).map(function() {
        return $(this).val();
        console.log(night);
      }).get(),
    ]

    alert(modess);

    const medic = [...morning, ...noon, ...night];
    console.log(medic);
    var medical = JSON.stringify(medic);
  });
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;table class=&quot;table table-bordered mb-0&quot; id=&quot;medical&quot;&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Medicine Name&lt;/th&gt;
      &lt;th&gt;Morning&lt;/th&gt;
      &lt;th&gt;Noon&lt;/th&gt;
      &lt;th&gt;Night&lt;/th&gt;
      &lt;th&gt;charge&lt;/th&gt;
      &lt;th&gt; &lt;button type=&quot;button&quot; name=&quot;add&quot; id=&quot;add&quot; class=&quot;btn btn-success btn-xs&quot;&gt; + &lt;/button&gt; &lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody id=&quot;rows&quot;&gt;&lt;/tbody&gt;
&lt;/table&gt;

<!-- end snippet -->

答案1

得分: 2

问题是因为您按输入分组,而不是按表中的行分组。

还请注意,您似乎在多个元素上使用相同的 id,这是无效的。它们必须是唯一的。而是在它们上面都使用一个共同的类。然后,您可以使用 map() 从这些字段中选择值,就像这样:

let modess = $('#rows tr').map(function() {
  let $tr = $(this);
  return [
    $tr.find('.morning').val(),
    $tr.find('.noon').val(),
    $tr.find('.night').val(),
  ];
});
英文:

The issue is because you're grouping by input, not by row in the table.

Also note that you appears to be using the same id on multiple elements, which is invalid. They have to be unique. Use a common class on them all instead. Then you can use map() to select the values from those fields like this:

let modess = $(&#39;#rows tr&#39;).map(function() {
  let $tr = $(this);
  return [
    $tr.find(&#39;.morning&#39;).val(),
    $tr.find(&#39;.noon&#39;).val(),
    $tr.find(&#39;.night&#39;).val(),
  ];
});

huangapple
  • 本文由 发表于 2020年1月4日 00:21:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581902.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定