如何使用JavaScript从HTML选择标签中获取值或文本?



在我的html文件中,我有以下内容:

<td id="trainers_rowC1">Trainer1</td>
<td><input type="button" id="edit_button1" value="Edit" class="edit" onclick="edit_rowC('1')"></td>

在我的JavaScript文件中,我有这个函数:

function edit_rowC(no) {
var trainers = document.getElementById("trainers_rowC" + no);
var trainers_data = trainers.innerHTML;
trainers.innerHTML = "<select id ='trainers_options" + no + "'><option value='" + trainers_data + "'>";
}

但是当我按下编辑按钮时下拉菜单是空的。错在哪里?

这对我很有用。

你的错误是你试图用value="..."设置选项标签的内容。但是你必须把它写在两个标签之间。——比;<option>" + test + "</option>

function edit_rowC(no) {
var test = document.getElementById("trainers_rowC" + no).innerHTML;
document.getElementById("trainers_rowC" + no).innerHTML = "<td><select id ='trainers_options" + no + "'><option>" + test + "</option><option>test</option></select></td>";
}

在训练器的分配中似乎没有任何结束标记。innerHTML -可能是它吗?

最新更新