首先:我的问题是,通过 DOM 将字符串插入到表单的多个字段中,这些值在开头和结尾都会出现大量空格。
我想做的事: 查找我双击的行中单元格的值,并将这些值放入表单的字段中。
HTML 相当于:
<form>
<fieldset>
<legend> Very Important </legend>
<label for="input_representing_all_the_inputs">
<input id="input_representing_all_the_inputs"></input>
</fieldset>
</form>
<table id="issue_table" class="sortable", border="1" >
<tr ondblclick="values_into_form(issue_table)">
<td>
a value
</td>
<td>
another one
<td>
</tr>
<tr ondblclick="values_into_form(issue_table)">
<td>
here's also another value
</td>
<td>
aaand one more.
</td>
</tr>
</table>
<table id="not_the_issue_table" class="sortable", border="1">
[...] blabla looks similar [...]
</table>
现在我有了一个 js 函数来从表中查找值并将它们放入表单中。
function values_into_form( id ) {
//find index of row with given id
for (i = 0; i<= document.getElementById("accl_content").rows.lenght; i++){
if ( document.getElementById("issue_table").rows[i].id == id) {
var idx = i;
break;
}
}
// now insert the values into the editor-form
document.getElementById("input_representing_all_the_inputs" =
document.getElementById("issue_table").rows[idx].cells[0];
// repeat the above for each input_field with the specific cell_idx.
}
}
现在,让我们看看 issues_table 的第一个。 该值是“一个值”。但是,输入字段将包含类似“a value”的内容。
我是否犯了一个特定的错误来产生这个? 我不想要一个消除空格的解决方案,我想要一个真正的解决方案,想说,我根本不希望它们发生。
我对 JS 很陌生,来自 lua。因此,如果我犯了一些概念错误,如果您能在旁注中告诉我,我会很高兴。
最佳答案
将您的 html 更改为:
<table id="issue_table" class="sortable", border="1" >
<tr ondblclick="values_into_form(issue_table)">
<td>a value</td>
<td>another one<td>
</tr>
<tr ondblclick="values_into_form(issue_table)">
<td>here's also another value</td>
<td>aaand one more.</td>
</tr>
</table>
空格是因为你的html中有空格。当然,您也可以使用 javascript 去除开头和结尾的空格。但如果您不想这样做,上述解决方案应该可行。
关于javascript - Rows-Element,这些空格从哪里来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30574746/