我正在使用x-editable
http://vitalets.github.io/x-editable/index.html一个 jquery 就地编辑器。目前我有一个工作代码如下:
<?php
$num_rows = 1;
// store the record of the "tblstudent" table into $row
while ($row = mysqli_fetch_array($result)) {
echo "<script type=\"text/javascript\">
$.fn.editable.defaults.mode = \"popup\";
$('#username$num_rows').editable();
</script> "; //assign num to element
// Print out the contents of the entry
echo '<tr>';
echo '<td><a href="#" id="username' . $num_rows . '" data-type="text" data-pk="' . $row['id'] . '" data-url="post.php" data-title="Edit website">';
echo htmlspecialchars($row['website'], ENT_QUOTES, 'UTF-8');
echo '</a></td>';
echo '</tr>';
$num_rows++;
}
?>
结果如下:
但是正如您所看到的,我使用 $num_rows
来分配元素 ID 并使用 javascript 获取 ID。我不喜欢使用循环将 uniq ID 分配给元素或在 php 标记中包含 javascript。还有比这更优雅的解决方案吗?
提前致谢。
最佳答案
将 id 保留为用户名,或者添加 class='username'
而不是 id。
<script type="text/javascript">
$.fn.editable.defaults.mode = "popup";
$('.username').click(function(){
$(this).editable();
})
</script>
关于javascript - 从同一 ID 名称分配和获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26516841/