douqianxun8540 2013-01-31 17:40
浏览 22
已采纳

更改行值onclick html表,如phpMyAdmin

I'm basically trying to use a simple method of editing table stored inside a mySQL database, but don't want to go through a different editing page, so my theory is :

Show all data inside a HTML table as usual would as followed.

<table class="table table-bordered sortable">
<thead>
        <tr>
                <th>Marchant</th>
                <th>URL</th>
                <th>Status</th>
                <th>Sold</th>
                <th>Deals</th>
                <th>Sites</th>
                <th>Found</th>
                <th>Seen</th>
        </tr>
</thead>

<tbody>
        <tr>
                <td>value</td>
                <td></td>
                <td></td>
                <td>Sold here</td>
                <td>Deals here</td>
                <td>Sites here</td>
                <td>null</td>
                <td>null</td>
        </tr>
</tbody>

I want to be able to edit the data on the fly, simply by clicking on the value displayed and changing it.

The question is how would i go about by doing this, is it even possible ?

  • 写回答

2条回答 默认 最新

  • douluokuang7184 2013-01-31 18:28
    关注

    Yes, its possible.

    It's mostly frontend.

    Here is example of creating input after you click on a cell. (jQuery)

    // once page is loaded
    $(document).ready(function() {
    
        // adding an event when the user clicks on <td>
        $('td').click(function() {
    
            // create input for editing
            var editarea = document.createElement('input');
            editarea.setAttribute('type', 'text');
    
            // put current value in it
            editarea.setAttribute('value', $(this).html());
    
            // rewrite current value with edit area
            $(this).html(editarea);
    
            // set focus to newly created input
            $(editarea).focus();
    
        });
    
    });
    

    After this you can add event to newly created input. (for example when user hits Enter)

    Then you do AJAX request and send new values to a .php script.

    You also need to add id to a newly created element so you know exactly what cell is needed to be changed after data is sent via AJAX.

    Also, don't forget to validate data before putting to MySQL.

    So if everything went good or not you return value back, and according to the value you write JavaScript code to to remove element from a cell / put edited value or popup a message.

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件