【问题标题】:Retrieve array in input field在输入字段中检索数组
【发布时间】:2016-09-29 21:14:18
【问题描述】:

我有这个功能:

  function AddRowToForm(row){
  $("#orderedProductsTblBody tr").each(function(){
  // find the first td in the row
  arr.push($(this).find("td:first").text());
  });
  for (i=0;i<arr.length;i++)
  // display the value in input field
document.getElementById("message").innerHTML = (arr[i] + "<br >");
 });
} 

问题是我必须将输入字段中的数组检索到表单中。输入字段是最后一个(id="message")

 fieldset class="pure-group">
  <label for="date">Data: </label>
  <input id="date" name="date" type="text"/>
</fieldset>

<fieldset class="pure-group">
  <label for="time">Ora: </label>
  <input id="time" name="time" />
</fieldset>

<fieldset class="pure-group">
  <label for="pax">Numero di Persone: </label>
  <input id="pax" name="pax" />
</fieldset>

<fieldset class="pure-group">
  <label for="message">Esperienze: </label>
  <input id="message" name="message" rows="5"></input>
</fieldset>

问题是该功能不起作用。我尝试使用 document.write (而不是 innerHTML,它可以工作,但它会取消所有内容。有什么帮助吗?提前谢谢你!:

【问题讨论】:

  • 那么你想要的输出是什么?输入没有innerHTML,输入行??
  • 尝试用value替换innerHTML,因为message是一个输入。
  • @epascarello 我想要表格第一列的文本值
  • @Santi 谢谢它的工作原理!

标签: javascript arrays forms input html-table


【解决方案1】:

所以你有几个问题。您将输入的文本设置为值。您也没有附加,您只是覆盖了该值。对于 textarea,您需要使用 \n 换行。

$("#orderedProductsTblBody tr").each(function(){
   arr.push($(this).find("td:first").text());
});
for (var i=0;i<arr.length;i++)
   // display the value in input field
   document.getElementById("message").value += arr[i] + "\n");
});

现在我该怎么做?只需使用连接。

document.getElementById("message").value = arr.join("\n");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-05
    • 2013-07-29
    • 1970-01-01
    • 2021-09-22
    • 2019-02-13
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    相关资源
    最近更新 更多