【发布时间】:2018-05-15 05:32:05
【问题描述】:
我有一个动态填充数据的表。每行在状态部分下都有一个下拉菜单,用户可以从中选择一个值,根据选择的值,必须显示一些输入框,但这些框应该显示在下拉列表所属的行中。因此,如果下拉列表属于第一行,则该框应仅出现在第一行,如果下拉列表属于第二行,则该框应仅出现在第二行中
目前,我使用的代码是在不同的行中随机显示输入框,而与下拉位置无关
谁能告诉我如何纠正这个问题 HTML 代码
<table class="table table-bordred table-striped">
<thead>
<th>NAME</th>
<th>STATUS</th>
</thead>
<tbody>
<?php foreach($student as $per_student): ?>
<tr>
<td><?php echo $per_student->name; ?></td>
<td>
<select class="can-dd" onchange="myFunction(this)" name="status">
<option value="">STATUS</option>
<option value="Accepted ">Accepted</option>
<option value="Forwarded ">Forwarded </option>
<option value="Schedule">Schedule </option>
<option value="Custom">Custom</option>
</select>
<div class="schedule">
<input type="text" placeholder="schedule" name="schedule">
<button type="submit" class="btn btn-primary " >SUBMIT</button>
</div>
<div class="custom">
<input type="text" placeholder="custom" name="custom">
<button type="submit" class="btn btn-primary " >SUBMIT</button>
</div>
<div class="submit_value">
<button type="submit" class="btn btn-primary " >SUBMIT</button>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
脚本
<script type="text/javascript">
function myFunction(selectObject) {
var value = selectObject.value;
if(value == 'Schedule'){
jQuery('.custom').remove();
jQuery('.submit_value').remove();
$(".schedule").show();
}
else if(value == 'Custom'){
jQuery('.schedule').remove();
jQuery('.submit_value').remove();
$(".custom").show();
}
else{
jQuery('.schedule').remove();
jQuery('.custom').remove();
$(".submit_value").show();
}
}
</script>
【问题讨论】:
-
ids 在文档中应该是唯一的。 -
@Teemu 你能详细说明一下吗
-
PHP
foreach循环使用硬编码的ids 创建trs 中的元素。当你引用像jQuery('#custom')这样的元素时,jQuery 只会从文档中找到第一个带有id的元素。 -
详细说明。每个元素只能有一个唯一 ID,否则当您尝试执行以该 ID 为目标的脚本时会导致错误。不能有两个元素共享同一个 ID。
-
您可以在此处阅读有关 html id 的更多信息:developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id
标签: javascript php jquery ajax