由于所选对象的字段始终显示第一个元素的属性,我该如何将所选对象的字段放入相应的表单中?这是我到目前为止所做的:
在我的操作表列上,我具有以下定义:<a href="#do=View?id=<?php echo $row['id']; ?>" onClick="manageFormView('view')"><i class="fas fa-eye cyan-text"></i></a>
,其中我的javascript函数的定义为:
function manageFormView(flag){
if(flag === 'create'){
document.getElementById("form3").value = "";
document.getElementById("form2").value = "";
manageCreateBtn(1);
manageEditDeleteBtn(1);
manageSaveCancelBtn(0);
}else if(flag === 'view'){
const name = document.querySelector('#dept-name');
document.getElementById("form3").value = name.dataset.columns;
const code = document.querySelector('#dept-code');
document.getElementById("form2").value = code.dataset.columns;
const id = document.querySelector('#dept-id');
document.getElementById("id").value = id.dataset.columns;
manageCreateBtn(1);
manageEditDeleteBtn(0);
manageSaveCancelBtn(1);
}else if(flag === 'edit'){
const name = document.querySelector('#dept-name');
document.getElementById("form3").value = name.dataset.columns;
const code = document.querySelector('#dept-code');
document.getElementById("form2").value = code.dataset.columns;
const id = document.querySelector('#dept-id');
document.getElementById("id").value = id.dataset.columns;
manageCreateBtn(1);
manageEditDeleteBtn(1);
manageSaveCancelBtn(0);
}else{
document.getElementById("form3").value = "";
document.getElementById("form2").value = "";
document.getElementById("id").value = "";
}
};
在我的表格上,我有这样的东西:
<form action="departments.php" class="was-validated" id="lt-create-form" method="post">
<div class="card">
<div class="card-block">
<div class="md-form">
<i class="fas fa-user prefix"></i>
<input type="text" id="form3" name="name" class="form-control">
<label for="form3">Department Name</label>
</div>
<div class="md-form">
<i class="fas fa-envelope prefix"></i>
<input type="text" id="form2" name="code" class="form-control">
<label for="form2">Code</label>
</div>
<input type="hidden" id="id" name="id" value="">
</div>
</div>
</form>
我是否错误定义了JavaScript函数?顺便说一句,我的列表视图和表单视图在同一文件departments.php
上。我得到的是下面的图片: