从数据库预填充模式(非引导)
Pre-fill modal from db (non-Bootstrap)
我正在尝试用数据库(sqlite)中的值预填充表单。
我在JSFiddle中模拟了我的代码:
https://jsfiddle.net/daikini/e71mvg7n/
这里还有一个陷阱。。。由于各种原因,我没有使用引导程序。所以,如果没有它也能实现,那就是偏好。
我有一个很好的数据库连接,我可以拉和张贴(使用PHP)。但是,我还没能把它拉进状态。
这是我的JS:
// Get the modal
var modal = document.getElementById('myModal');
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
任何建议都会很有帮助。非常感谢。
编辑:
再次感谢您在这方面的帮助。当我尝试使用PHP和Javascript预填充表单(在模态中)时,我仍然会遇到问题。以下是相关代码:
<?php
foreach($result as $row)
{
echo "<tr>";
echo "<td>" . $row['job_id'] . "</td>";
echo "<td>" . $row['job_title'] . "</td>";
echo "<td>" . $row['date_create'] . "</td>";
echo "<td><a href='#' id='editbtn". $row['job_id']."' class='edit-button' name='edit". $row['job_id']."' data-value='".json_encode($row, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE)."'>EDIT</a></td>";
if ($row['display']) {
echo '<td><form method="post" action="update_jobs.php"><input type="checkbox" name="'. $row['job_id'] . '" value="'. $row['display'] .'" checked />';
} else {
echo '<td><form method="post" action="update_jobs.php"><input type="checkbox" name="'. $row['job_id'] . '" value="'. $row['display'] .'" />';
}
echo '<td class="last"><input type="submit" value="Submit" name="active" data-toggle="modal" data-target="#editModal" ></form></td>';
echo "</tr>";
}
echo "</table>";
echo $row['position_summary'];
?>
和JS:
<script type="text/javascript">
var editModal = document.getElementById('editModal');
var editbtn = document.getElementById("editbtn" + job_id).value;
jQuery('.edit-button').click(function() {
id = jQuery(this).attr('data-abstract-id');
});
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on the button, open the modal
editbtn.onclick = function() {
var _data = $(this).data('value');
editModal.style.display = 'block';
console.log(_data);
setTimeout(function() {
$('#job-id').val(_data['job_id']);
$('#date-created').val(_data['date_create']);
$('#job_title').val(_data['job_title']);
$('#position_summary').text(_data['position_summary']);
$('#duty1').val(_data['duty1']);
$('#duty2').val(_data['duty2']);
$('#duty3').val(_data['duty3']);
$('#duty4').val(_data['duty4']);
$('#duty5').val(_data['duty5']);
$('#duty6').val(_data['duty6']);
$('#duty7').val(_data['duty7']);
$('#duty8').val(_data['duty1']);
$('#edu1').val(_data['edu1']);
$('#edu2').val(_data['edu2']);
$('#edu3').val(_data['edu3']);
$('#edu4').val(_data['edu4']);
$('#edu5').val(_data['edu5']);
$('#edu6').val(_data['edu6']);
$('#edu7').val(_data['edu7']);
$('#edu8').val(_data['edu8']);
}, 500);
}
// When the user clicks on <span> (x), close the modal
$(document).on('click', '.toggle-close-edit-modal', function () {
editModal.style.display = 'none';
});
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target.id == 'editModal') {
editModal.style.display = 'none';
}
}
</script>
这取决于何时使用预定义值填充表单。如果你想在页面加载时加载它,你可以用PHP生成HTML,并将数据打印到所需输入的value="标记中。
或者,如果您使用ajax加载数据,您可以使用jQuery来填充元素值。
$("form input[name='job_title']").val(value);
如果这不是你想要的,请澄清你的问题。
编辑:
我会简短地说,仍然不能100%确定你想做什么。
但我注意到,您从.edit按钮数据值加载JSON编码的数据,但希望将其作为对象进行访问。你应该先解码。
var _data = $.parseJSON($(this).data('value'));
试着详细说明你的问题是它仍然不起作用,你想实现什么,并尝试在某个地方上传一个有效的例子。
相关文章:
- 从数据库预填充模式(非引导)
- 使用模式消息/窗口显示数据库记录
- 如何获取所有旅行模式的距离和持续时间,例如驾驶,公交,骑自行车,在我的页面中步行以及将其存储在数据库中
- 流星技术/模式,用于等待数据库变量更改,然后在 in 之后执行某些操作
- Node.js+Mongoose:与数据库逻辑分离的模型/模式
- GraphQL模式的图形数据库设计
- 将数据库行“馈送”到接口的模式
- 获取在模式中输入的数据以保存在数据库中
- 为登录/注册模式实现正确的php数据库访问
- 从引导模式获取值,进行处理,然后将值插入数据库
- 从数据库(NodeJS)中的搜索返回Regex模式
- 在spring MVC中使用ajax从数据库中获取数据,在引导模式弹出框字段值中放置值
- 使用高效的主键模式在SQL数据库中存储2D映射
- 更新或修复AppFog数据库的模式
- 如何检索数据库弹出模式后点击jquery
- 设计用于处理电子邮件系统中类似颜色的数据库模式
- 具有数据库中特定信息的模式,具体取决于用户单击的位置
- Mongoose数据库连接和模式
- node.js设计模式,用于创建一次数据库连接
- 如何在数据库中存储猫鼬模式