gpt4 book ai didi

javascript - 在数据表中使用 onchange 函数

转载 作者:行者123 更新时间:2023-11-30 19:22:21 25 4
gpt4 key购买 nike

我正在使用 Datatables 在表格中创建表单。我有一个选择类型的输入,该输入的值会根据另一个输入而改变。我现在遇到的问题是它只适用于第一行。 Here是我的测试用例。

<table id="example" class="display nowrap" width="100%">
<thead>
<tr>
<th>ID</th>
<th>select 1</th>
<th>select 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>
<select id="input1" class="form-control">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
<td><select id="input2"></select></td>
</tr>
<tr>
<td>2</td>
<td>
<select id="input1" class="form-control">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
<td><select id="input2"></select></td>
</tr>
<tr>
<td>3</td>
<td>
<select id="input1" class="form-control">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
<td><select id="input2"></select></td>
</tr>
<tr>
<td>4</td>
<td>
<select id="input1" class="form-control">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
<td><select id="input2"></select></td>
</tr>
<tr>
<td>5</td>
<td>
<select id="input1" class="form-control">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
<td><select id="input2"></select></td>
</tr>
</tbody>
</table>

非常感谢对此事的任何帮助:)

最佳答案

用这段代码替换你的代码:

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>

<link href="https://nightly.datatables.net/css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
<script src="https://nightly.datatables.net/js/jquery.dataTables.js"></script>

<meta charset=utf-8 />
<title></title>
</head>
<body>
<div class="container">
<table id="example" class="display nowrap" width="100%">
<thead>
<tr>
<th>ID</th>
<th>select 1</th>
<th>select 2</th>
</tr>
</thead
<tbody>
<tr>
<td>1</td>
<td>
<select id="input1" data-attr="1" class="form-control selectBox">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
<td><select id="selectedInput1"></select></td>
</tr>
<tr>
<td>2</td>
<td>
<select id="input2" data-attr="2" class="form-control selectBox">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
<td><select id="selectedInput2"></select></td>
</tr>
<tr>
<td>3</td>
<td>
<select id="input3" data-attr="3" class="form-control selectBox">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
<td><select id="selectedInput3"></select></td>
</tr>
<tr>
<td>4</td>
<td>
<select id="input4" data-attr="4" class="form-control selectBox">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
<td><select id="selectedInput4"></select></td>
</tr>
<tr>
<td>5</td>
<td>
<select id="input5" data-attr="5" class="form-control selectBox">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
<td><select id="selectedInput5"></select></td>
</tr>
</tbody>
</table>
</div>
<script>
$(document).ready(function () {
var table = $('#example').DataTable();
$("#example tbody tr td .selectBox").on("change", function () {
var s1 = document.getElementById($(this).attr('id'));
var s2 = document.getElementById("selectedInput" + $(this).attr('data-attr'));
s2.innerHTML = "";
if (s1.value == "1") {
var optionArray = ["|--Select--", "ABC|ABC", "DEF|DEF", "GHI|GHI", ]
} else if (s1.value == "2") {
var optionArray = ["|--Select--", "JKL|JKL", "MNO|MNO", "PQR|PQR", ]
} else if (s1.value == "3") {
var optionArray = ["|--Select--", "STU|STU", "VWX|VWX", "YZ|YZ", ]
}

for (var option in optionArray) {
var pair = optionArray[option].split("|");
var newOption = document.createElement("option");
newOption.value = pair[0];
newOption.innerHTML = pair[1];
s2.options.add(newOption);
}
});
});
</script>
</body>
</html>

关于javascript - 在数据表中使用 onchange 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57319736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com