一 绑定回车事件:
jquery:
$('#dataInput').bind('keypress',function(event){
if(event.keyCode == "13")
{
alert('你输入的内容为:' + $('#dataInput').val());
}
});
原生js:
document.onkeydown = function(e){
if(!e){
e = window.event;
}
if((e.keyCode || e.which) == 13){
//具体操作
}
}
二 点击文本,直接变成可编辑状态,同时修改数据库内容
var clicknum = 0;
$('#changeName').click(function(){
if (clicknum == 0) {
clicknum ++;
var oldhtml = document.getElementById('changeName').innerHTML;
var newobj = document.createElement('input');
newobj.type = 'text';
newobj.value = oldhtml;
newobj.className = 'form-control';
newobj.style.width = '100px';
newobj.style.display = 'inline-block';
//回车事件
document.onkeydown = function(e){
if(!e){
e = window.event;
}
if((e.keyCode || e.which) == 13){
clicknum = 0;
document.getElementById('changeName').innerHTML = this.value == oldhtml ? oldhtml : this.value;
//异步修改数据库客户名称
var newName = document.getElementById('changeName').innerHTML;
if (newName != oldhtml && newName) {
ajax_do('__APP__?m=customer&a=updateCustomerName&id={$customer.customer_id}&name='+newName,"");
};
}
}
//鼠标事件
newobj.onblur = function() {
clicknum = 0;
document.getElementById('changeName').innerHTML = this.value == oldhtml ? oldhtml : this.value;
//修改数据库客户名称
var newName = document.getElementById('changeName').innerHTML;
if (newName != oldhtml && newName) {
ajax_do('__APP__?m=customer&a=updateCustomerName&id={$customer.customer_id}&name='+newName,"");
};
}
document.getElementById('changeName').innerHTML = '';
document.getElementById('changeName').appendChild(newobj);
newobj.setSelectionRange(0, oldhtml.length);
newobj.focus();
};
});
//通用ajax提交方式
function ajax_do(url,param,show_confirm ,confirm_title,confirm_text){
if(show_confirm){
swal({
title: confirm_title,
text: confirm_text,
type: "info",
confirmButtonText:'确定',
cancelButtonText:'取消',
showCancelButton: true,
closeOnConfirm: false,
showLoaderOnConfirm: true,
},
function(){
$.post(url+'&t=' + Math.random(1000),param, function(data){
swal.close();
parent.layer.open({
type: 1,
title: false,
closeBtn: false,
shadeClose:true, //点击遮罩层 关闭
content: data,
end: function(index){
}
});
}, 'html');
});
}else{
$.post(url+'&t=' + Math.random(1000),param, function(data){
parent.layer.open({
type: 1,
title: false,
closeBtn: false,
shadeClose:true, //点击遮罩层 关闭
content: data,
end: function(index){
//parent.layer.closeAll();
}
});
}, 'html');
}
}