如何从表中提取所有输入&#34; <select>&#34;并填充数组?

时间:2016-05-11 16:53:39

标签: jquery html json

我相当接近,但顺序完全错了。此时输出全部搞砸了,我似乎无法将所有行添加到数组中 $(&#39;#clicker&#39;)。on(&#39;点击&#39;,功能(e){          var tableToObj = function(table){              var trs = table.rows,         trl = trs.length,         i = 0,         j = 0,         keys = [],         obj,ret = [];       // j + = 1;              for(; i&lt; trl; i ++){                  if(i == 0){                      for(; j&lt; trs [i] .children.length; j ++){                          var sel = $(trs [i] .children [j])。find(&#34; select&#34;);                          if(sel.length == 0){                              keys.push(TRS [I]。儿童[j]的.innerHTML);                          } else {                              keys.push(sel.val());                          }                      }                  } else {                      obj = {};                      for(j = 0; j&lt; trs [i] .children.length; j ++){                         var sel = $(trs [i] .children [j])。find(&#34; select&#34;);                         if(sel.length == 0){                          obj [keys [j]] = trs [i] .children [j] .innerHTML;                      } else {                          keys.push(sel.val());                      }                      }                      ret.push(OBJ);                  }              }              返回;          };          document.getElementById(&#39; r&#39;)。innerHTML = JSON.stringify(tableToObj(document.getElementsByTagName(&#39; table&#39;)[0]));      });  }); 我正在使用的HTML如下:     &LT;表&gt;     &LT; TR&GT;&LT; TD&GT; FirstColumn&LT; / TD&GT;&LT; TD&GT; SecondColumn&LT; / TD&GT;&LT; TD&GT; ThirdColumn&LT; / TD&GT;&LT; / TR&GT;     &lt; tr&gt;&lt; td&gt;&lt; select&gt;&lt; option value =&#34; tr1&#34;&gt; tr1&lt; / option&gt;&lt; option value =&#34; tr2&#34;&gt; tr2&lt; /选项&gt;&lt;选项值=&#34; tr3&#34;&gt; tr3&lt; /选项&gt;&lt;选项值=&#34; tr4&#34;&gt; tr4&lt; /选项&gt;&lt; / select&gt;&lt; / td&gt;&lt; td&gt; 1&lt; / td&gt;&lt; td&gt;&lt; select&gt;&lt; option value =&#34; tr1&#34;&gt; tr1&lt; / option&gt;&lt; option value =&#34; tr2&# 34;&gt; tr2&lt; / option&gt;&lt; option value =&#34; tr3&#34;&gt; tr3&lt; / option&gt;&lt; / select&gt;&lt; / td&gt;&lt; / tr&gt;     &lt; tr&gt;&lt; td&gt; tr3 row&lt; / td&gt;&lt; td&gt;&lt; / td&gt;&lt; td&gt;&lt; select&gt;&lt; option value =&#34; tr1&#34;&gt; tr1&lt; / option&gt ;&lt;选项值=&#34; tr2&#34;&gt; tr2&lt; /选项&gt;&lt;选项值=&#34; tr3&#34;&gt; tr3&lt; /选项&gt;&lt;选项值=&#34; TR4&#34;&GT; TR4&LT; /选项&GT;&LT; /选择&GT;&LT; / TD&GT;&LT; / TR&GT;     &lt; tr&gt;&lt; td&gt; tr4 row&lt; / td&gt;&lt; td&gt;&lt; / td&gt;&lt; td&gt;&lt; / td&gt;&lt; / tr&gt;     &LT; TR&GT;&LT; TD&GT; 0℃; / TD&GT;&LT; TD→1&LT; / TD&GT;&LT; TD&GT; 0℃; / TD&GT;&LT; / TR&GT;     &LT; TR&GT;&LT; TD&GT; 0℃; / TD&GT;&LT; TD→1&LT; / TD&GT;&LT; TD&GT; 0℃; / TD&GT;&LT; / TR&GT; &LT; /表&gt;        &lt; button id =&#34; clicker&#34;&gt; Button&lt; / button&gt;     &lt; br /&gt;     结果:     &lt; div id =&#34; r&#34;&gt;&lt; / div&gt; 这是我得到的输出: [{&#34; SecondColumn&#34;:&#34; 1&#34;},{&#34; FirstColumn&#34;:&#34; tr3 row&#34;,&#34; SecondColumn&#34;: &#34;&#34;},{&#34; FirstColumn&#34;:&#34; tr4 row&#34;,&#34; SecondColumn&#34;:&#34;&#34;,&# 34; ThirdColumn&#34;:&#34;&#34;},{&#34; FirstColumn&#34;:&#34; 0&#34;&#34; SecondColumn&#34;:&#34; 1& #34;&#34; ThirdColumn&#34;:&#34; 0&#34;},{&#34; FirstColumn&#34;:&#34; 0&#34;&#34; SecondColumn&#34; :&#34; 1&#34;&#34; ThirdColumn&#34;:&#34; 0&#34;}] 这在改变选择值时不会改变。我非常有信心问题是for循环中的var sel。 谢谢

1 个答案:

答案 0 :(得分:0)

我认为有两件事需要解决:

  1. 要获取select代码的值,您应该将sel.val()的两个实例更改为sel.find('option:selected').val()

  2. 要解决排序问题,您需要在行循环的其他部分更新keys.push(obj[keys[j]] =(当i> 0时)

  3. JSFiddle Example