javascript - 从同一 ID 名称分配和获取值

标签 javascript php jquery getelementbyid x-editable

我正在使用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++;
    }
?> 

结果如下:

enter image description here

但是正如您所看到的,我使用 $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/

相关文章:

php - 获取所有以字符串开头的表名

除非我禁用监听按钮,否则不会触发带有 PhpStorm 断点的 PHPUnit Xdebug

jquery - 在CSS中删除带有动画效果的线条

javascript - 拖动图像以调整位置,就像在 Facebook 缩略图个人资料图片中一样

javascript - 导入 Vue 组件

javascript - 具有动态表单的 jquery 验证插件

javascript库显示相册

php - 读取 HTTP 响应 header 中的 Set-Cookie 指令

jquery - 如何对 jQuery 键盘事件进行单元测试?

javascript - 如何在一页中使用多个 pagedown?