douzhuochao4027 2015-01-07 14:14
浏览 37
已采纳

将选中的行转换为输入字段

I am showing data from database into table with the checkbox in front of every row. All I want is to change the checked rows columns with class click into input fields on clicking the edit button. I am very new to jQuery. I don't have much concepts about it. So help me please.

Here is the jQuery code I've tried:

$(function () { 
    $("input[name='check[]']:checked").each(function (){                
        $('#edit').click(function(){    
            var OriginalContent = $('.click').text(); 
            $('.click').addClass("cellEditing"); 
            $('.click').html("<input type='text' value='" + OriginalContent + "' />"); 
            $('.click').children().first().focus(); 

            $('.click').children().first().keypress(function (e) { 
                if (e.which == 13) { 
                    var newContent = $('.click').val(); 
                    $('.click').parent().text(newContent); 
                    $('.click').parent().removeClass("cellEditing"); 
                    }

            }); 

            $('.click').children().first().blur(function(){ 
            $('.click').parent().text(OriginalContent); 
            $('.click').parent().removeClass("cellEditing"); 
            });
    });
}); 

});

And here is the html:

echo '<div class="table-responsive">';
echo '<table class="table table-bordered table-hover"><thead><tr>
<th>ID</th><th>Name</th><th>Category</th><th>Description</th><th>Price</th><th>Status</th><th colspan="2">Image</th></tr></thead><tbody>';

while($row = mysqli_fetch_array($retval))
{
echo '<tr><td>'.$row["ID"].'</td>
<td >'.$row["Name"].'</td>
<td>'.$row["Category"].'</td>
<td>'.$row["Description"].'</td>
<td class="click1">'.$row["Price"].'</td>
<td class="click">'.$row["Status"].'</td>
<td><img style="width: 3em; heigth: 3em;" class="img-responsive" src="'.$row["Imagepath"].'" alt="'.$row["Name"].'"/></td>
<td><input class="checkbox" name="check[]" id="check[]" type="checkbox" value='.$row["ID"].'/></td></tr>';

} 
echo '</tbody></table>';
echo '</div>';

?> 
<center><input class="btn btn-default" name="deletebutton" type="submit" value="DELETE" />
<input class="btn btn-default" name="edit" id="edit" type="button" value="EDIT" />
<input class="btn btn-default" name="updatebutton" type="submit" value="UPDATE"/></center>
</form>
  • 写回答

2条回答 默认 最新

  • duanlaiyin2356 2015-01-07 15:49
    关注

    Basically you need two functions to run...

    1. when "edit" is clicked: convert all "editable" columns to INPUTs, if that row is ":checked"
    2. when ENTER is pressed while editing one of your "editable" INPUTs: revert it to a string

    I simplified your HTML a bit for clarity. An explanation of the jQuery needed is inline.

    Also, here's a working jsFiddle: http://jsfiddle.net/msz76tgp/2/

    HTML

    <table>
        <tr>
            <td class="editable status">something</td>
            <td class="editable price">2</td>
            <td class="checkbox"><input type="checkbox"/></td>
        </tr>
        <tr>
            <td class="editable status">something else</td>
            <td class="editable price">3</td>
            <td class="checkbox"><input type="checkbox"/></td>
        </tr>
    </table>
    <a id="edit" href="#edit">edit</a>
    

    jQuery

    $(function () { 
    
        // When edit is clicked...
        $('#edit').on('click', function(){
    
            // For each ":checked" checkbox...
            $('.checkbox INPUT:checked').each( function(){
    
                // Get the parent row...
                var $row = $(this).closest('tr');
    
                // And that row's "editable" columns...
                var $editable= $row.children('.editable');
    
                // Add the "editing" class...
                $editable.addClass('editing');
    
                // And change all strings to INPUT fields.
                $editable.each( function(){ 
                    $(this).html('<input value="'+$(this).text()+'"/>');
                });
    
            });
    
        });
    
    
        // Also...
        // Any time an .editing INPUT receives a keypress...
        $('table').on('keypress', '.editing', function(e){
    
            // If the key pressed is ENTER...
            if( e.which == 13 ){
    
                // Get the current row...
                var $row = $(this).closest('tr');
    
                // Get the editable columns...
                var $editable = $row.children('.editable');
    
                // Remove the class...
                $editable.removeClass('editing');
    
                // Change back to string...
                $editable.each( function(){ 
                    $(this).html( $(this).find('INPUT').val() );
                });          
    
            }
    
        });
    
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥18 光催化第一性原理计算析氢效率STH怎么计算
  • ¥100 Mac 版foxmail 收邮件问题
  • ¥15 QWebEngineView
  • ¥15 如何使用shufflenet进行手写数字识别
  • ¥15 .net core 同时编辑怎么防止数据串了
  • ¥20 微信小程序播放直播流
  • ¥15 关于迷宫自走单片机循迹小车的知识
  • ¥15 python使用selenium工具爬取网站的问题
  • ¥15 visual studio中c语言用ODBC链接SQL SERVER
  • ¥15 关于#python#的问题:如何通过pywinauto获取到图中“窗格”内部的内容