【发布时间】:2019-06-21 09:15:49
【问题描述】:
我有一张客户表。每个客户都有名字和姓氏。表格的两个文本字段是可编辑的。因此,用户可以在按下保存时更新信息。问题是我无法获取具体的行信息,我只能获取第一行结果
我尝试使用输入字段匹配名称,但没有成功。
<?php foreach($customer as $each){ ?>
<td class="first_name" id="first" contenteditable="true"><?php echo
$each['first_name']; ?></td>
<td class="last_name" id="last" contenteditable="true"><?php echo
$each['last_name']; ?></td>
<td > <button type="button" onclick="save('<?php echo $each['first_name'];?
>','<?php echo $each['last_name'];?>');" >Save</button></td>
<? } ?>
<script type="text/javascript">
function save(first,second) {
<?php foreach($customer as $each){?>
var first_name = "<?php echo $each['first_name']?>";
var last_name = "<?php echo $each['last_name']?>";
if (first_name==first && last_name == second){
var fname = document.querySelectorAll(".first_name");
console.log(fname[0]);
}
<?php } ?>
}
</script>
【问题讨论】:
-
那是因为当你做
this.document.querySelector('#first_name'),时,你总是得到第一个名字。您应该查找/保存您在函数参数中获得的名称 -
这是因为您使用了 ID。 ID 是唯一的,因此您只能在文档中使用它们一次。您应该使用类属性来实现这一点。
-
尝试使用类而不是id的
.first-name.last-name -
@AndrejsGubars 我编辑了代码并添加了类并添加了 querySelectorAl。不过,我只得到第一行结果
标签: javascript php jquery html-table