[英]How to write an onClick event
while($row = $result->fetch_assoc()) {
echo "<tr><td>". $row["moduleCode"]. "</td><td>". $row["moduleTitle"]. "</td></tr>";
}
echo "</table>";
}
This is part of my php code.这是我的 php 代码的一部分。 How could I write an 'onClick' event so the 'moduleCode' is put into an input tag?我如何编写“onClick”事件以便将“moduleCode”放入输入标签中?
Are you looking for a solution with or without a library?您是否正在寻找带或不带库的解决方案? Do you want to register the event later or do you like to use onclick=""?您想稍后注册该事件还是喜欢使用 onclick=""?
A simple solution for onclick="" on a button, or whatever should be your click target, could look like this:按钮上 onclick="" 的简单解决方案,或者任何应该是您的点击目标的东西,可能如下所示:
<?php
$rows = [
[
'moduleTitle' => 'test',
'moduleCode' => 'testcode'
]
];
echo "<table>";
foreach ($rows as $key => $row) {
echo '<tr>'
. ' <td id="code_' . $key . '">' . $row['moduleCode'] . '</td>'
. ' <td>' . $row['moduleTitle']. '</td>'
. ' <td><button tpye="button" onclick="copyToInput(' . $key . ')">copy code</button></td>'
. '</tr>';
}
echo '</table>';
echo '<input type="text" name="target" id="target" value="">';
?>
<script>
function copyToInput(key) {
codeElem = document.querySelector('#code_' + key);
if (codeElem) {
targetElem = document.querySelector('#target');
targetElem.value = codeElem.innerHTML;
}
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.