1 回答
TA贡献1815条经验 获得超6个赞
每当您尝试从 HTML 执行此操作时,请尝试使用trim()多余的空格,因为.innerHTMLHTML 不是空格敏感的,但 JavaScript 是:
document.getElementById("mySelect").value = selectedRow.cells[1].innerHTML.trim();
// ^^^^^^^
这将使它发挥作用。片段在这里:
function myFunction(td) {
selectedRow = td.parentElement.parentElement;
document.getElementById("mySelect").value = selectedRow.cells[1].innerHTML.trim();
}
<form>
Select your favorite fruit:
<table>
<tr>
<td>
1
</td>
<td>
orange
</td>
</td>
<td>
<button type="button" onclick="myFunction(this)">Try it</button>
</td>
</tr>
</table>
<select id="mySelect">
<option value="apple">Apple</option>
<option value="orange">Orange</option>
<option value="pineapple">Pineapple</option>
<option value="banana">Banana</option>
</select>
</form>
添加回答
举报