单击文本之后变输入框做ajax保存加强版
html页面代码如下:
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
<span class="price" style="display: inline;">20.00</span>
<span class="textInput" style="display: none;">
<input type="hidden" name="id" value="1">
<input name="price" type="text" value="20.00">
<span class="postPrice">aaaa</span>
</span>
<script>
$(function(){
$(".price").click(function(){
$(this).hide();
$(this).next().fadeIn('slow');
})
$(".postPrice").click(function(){
var thisPrice = $(this);
var price = thisPrice.parent().find('input[name="price"]').val();
var id = thisPrice.parent().find('input[name="id"]').val();
var url = 'http://www.xxx.com/index.php?s=Plus-Stars-ajaxSave';
$.post(url,{price:price,id:id},function(data){
if(data){
thisPrice.parent().hide();
thisPrice.parent().prev().html(price).fadeIn();
}else{
thisPrice.parent().html('保存失败');
}
})
})
})
</script>
php异步接收更改数据库页面程序save.php:
public function ajaxSave(){
$data['price'] = intval($_POST['price']);
echo $data['price'];
//$res = M('course')->where('id = '.intval($_POST['id']))->save($data);
echo $res;
}