jquery的即点即改

    <table border="">  
        <th>编号</th>  
        <th>用户名</th>  
        <th>密码</th>  
        <?php foreach($i as $v){ ?>  
        <tr>  
            <td><?php echo $v['id']?></td>  
            <td value="<?php echo $v['id']?>"><span class="name"><?php echo $v['name']?></span></td>  
            <td><?php echo $v['pwd']?></td>  
        </tr>  
        <?php } ?>  
    </table>  

<script>

    $(document).on('click','span',function(){
         old_val=$(this).html();
        $(this).parent().html("<input type=\'text\' value="+old_val+">");
        $('input').focus();
    })

    $(document).on('blur','input',function(){
        var obj=$(this);
        var id=$(this).parent().attr('value'); //获取要修改内容的id
        var val=$(this).val(); //获取修改后的值
        $.ajax({
            type:'post',
            url:'index.php/welcome/upd_pro',
            data:{
                id:id,
                val:val
            },
            success:function(msg){
                if(msg == 1){
                    $('td[value='+id+']').parent().html("<span class='name'>"+val+"</span>")
                }else{
                    obj.parent().html("<span class='name'>"+old_val+"</span>")
                }

            }
        })
    })

/***************************************
<pre name="code" class="html">$(document).on('click','span',function(){
    var old_val=$(this).html();
    $(this).parent().html("<input type=\'text\' value="+old_val+">");
    $('input').focus();

    $('input').blur(function(){
        var obj=$(this);
        var id=$(this).parent().attr('value'); //获取要修改内容的id
        var val=$(this).val(); //获取修改后的值
        $.ajax({
            type:'post',
            url:'index.php/welcome/upd_pro',
            data:{
                id:id,
                val:val
            },
            success:function(msg){
                if(msg == 1){
                    obj.parent().html("<span class='name'>"+val+"</span>")
                }else{
                    obj.parent().html("<span class='name'>"+old_val+"</span>")
                }

            }
        })
    })

})


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值