jquery即点即改

表单页面

<td><span class="dian" id="<?php echo $val['g_id']?>"><?php echo $val['goods_price']?></span></td>
表单页面下的jquery

<script>
    $(function(){
        //击点即改
        $(document).on("click",".dian",function(){//给属性为class的属性一个点击事件
             var goods_price=$(this).html();//获取span标签里的值
             var id=$(this).attr('id');//获取span标签里属性对应的id
             var _this=$(this);//给span定义一个变量
            _this.parent().html("<input type=\"text\" value="+goods_price+" id="+id+" class='one'/>");//把td变成文本框
             var inp=$('.one').focus();//获取焦点
            //失去焦点
             inp.blur(function(){
                 var new_price=$(this).val();//获取文本框里的新值
                 var id=$(this).attr('id');//获取id
                 $.post("?r=home/upd",{id:id,price:new_price},function(msg){
                    if(msg==1){
                       inp.parent().html("<span class=\"dian\" id="+id+">"+new_price+"</span>");
                    }
                     else{
                       inp.parent().html("<span class=\"dian\" id="+id+">"+goods_price+"</span>");
                    }
                 });
             });

         });
    });
</script>
控制器层里方法

//击点即改
public function actionUpd(){
    $arr=\Yii::$app->request->post('price');
    $id=\Yii::$app->request->post('id');
    $data=\Yii::$app->db->createCommand("update goods set goods_price='$arr' where g_id='$id'")->execute();
     if($data){
         return 1;
     }
    else{
        echo 0;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值