[英]How to get value from select box in html table
I'm trying to retrieve a value from a select box in my HTML table.我正在尝试从我的 HTML 表格中的选择框中检索一个值。 How can I select the selected value?
如何选择所选值?
What I've tried so far:到目前为止我已经尝试过:
this.cells[14].innerHTML;
this.cells[14].children[0].innerhtml;
this.cells[14].children[0].innerText;
The last one .innerText returns all the items in my select box......
var table2 = document.getElementById('table_items_program');
for (var i2 = 1; i2 < table2.rows.length; i2++) {
table2.rows[i2].onclick = function () {
document.getElementById("id").value = this.cells[0].innerHTML;
document.getElementById("select").value = this.cells[14].children[0].innerText;
}
}
<td><select class="selectpicker" multiple>
<?php while ($row9 = mysqli_fetch_assoc($result9)):; ?>
<option value="<?php echo $row9['id']; ?>"><?php echo $row9['id'];?>
</option>
<?php endwhile; ?>
</select></td>
<input style="display: inline" id="id" name="id">
<input style="display: inline" id="select" name="select">
The selected value from my selection box in my HTML table从我的 HTML 表格中的选择框中选择的值
How can I select the selected value?
如何选择所选值?
You can get the selected index with the method HTMLSelectElement.selectedIndex
您可以使用
HTMLSelectElement.selectedIndex
方法获取选定的索引
But if I understand your trouble.但如果我理解你的麻烦。 You try to get the current values of your select.
您尝试获取您选择的当前值。 So you need to get the value of your selected options.
所以你需要得到你选择的选项的值。
You can do it like:你可以这样做:
document.getElementById('mySelect').onchange = function() { let options = this && this.options; let selectedValues = [] for (var i = options.length; i--;) { let opt = options[i]; if (opt.selected) { selectedValues.push(opt.value || opt.text); } } console.log(selectedValues) }
<select id="mySelect" multiple> <option value="a"> a </option> <option value="b"> b </option> <option value="c"> c </option> </select>
Implementation into your code example在您的代码示例中实施
for (var i2 = 1; i2 < table2.rows.length; i2++) {
table2.rows[i2].onclick = function() {
document.getElementById("id").value = this.cells[0].innerHTML;
var options = this && this.cells[14].children[0].options;
var selectedValues = []
for (var i = options.length; i--;) {
var opt = options[i];
if (opt.selected) {
selectedValues.push(opt.value || opt.text);
}
}
document.getElementById("select").value = JSON.stringify(selectedValues);
}
}
Welcome to Stack Overflow!欢迎来到堆栈溢出!
So, your <select>
has the class selectpicker
.所以,你的
<select>
有类selectpicker
。 We can use this to select your <select>
!我们可以用它来选择你的
<select>
!
Try the following to get your value:尝试以下方法来获得您的价值:
document.querySelector(".selectpicker").value;
Or, if you have multiple <select>
's, you can use或者,如果您有多个
<select>
,您可以使用
[...document.querySelectorAll(".selectpicker")].forEach(opt => {
console.log(opt.value)
});
Hope that clears things up a little.希望能把事情搞清楚一点。
so simple...很简单...
ShowSelected.onclick = function() { console.clear() document.querySelectorAll('.selectpicker option:checked').forEach(opt=>console.log(opt.value)) }
.selectpicker { vertical-align: text-top; }
choose one or more: <select class="selectpicker" multiple size=6 > <option value="a"> A element</option> <option value="b"> B element</option> <option value="c"> C element</option> <option value="d"> D element</option> <option value="e"> E element</option> <option value="f"> F element</option> </select> <button id="ShowSelected"> Show Selected</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.