ajax如何在jquery使用,如何在jQuery中使用Ajax

$(document).ready(function(){

if (getParameterByName(\'t\') == \'\'){

loadModal();

}else{

enableButton();

}

$(\"#btnNew\").click(function(){

clearModal();

$(\"#employee_modal\").modal(\'show\');

});

$(\".save\").click(function(){

if ($(\"#id\").val() == 0){

ajaxRequest(\"controller/event.php?event=save\", \'POST\', $(\"#form1\").serialize(), \"save\");

}else{

ajaxRequest(\"controller/event.php?event=update\", \'POST\', $(\"#form1\").serialize(), \"update\");

}

});

$(\".delete\").click(function(){

var _this=$(this).parent().parent();

var ID=_this.attr(\'data-id\');

var ans = confirm(\'Are you sure you want to delete this employee?\');

if (ans == true) {

ajaxRequest(\"controller/event.php?event=delete\", \'POST\',\'id=\'+ID, \"delete\");

}

});

$(\".edit\").click(function(){

var _this=$(this).parent().parent();

var ID=_this.attr(\'data-id\');

ajaxRequest(\"controller/event.php?event=edit\", \'POST\',\'id=\'+ID, \"edit\");

});

});

function loadModal(){

$(\"#greeting\").modal(\'show\');

}

function loadModal2(){

$(\"#employee_modal\").modal(\'show\');

}

function getData(url,type,data){

var jsonData = null;

$.ajax({

url: url,

dataType: \"json\",

data:data,

type: type,

async: false,

success: (

function(data) {

jsonData = data;

}),

error: function(xhr,status,error){

}

});

return jsonData;

}

function enableButton(){

var activeForm = getParameterByName(\'t\');

switch (activeForm){

case \"employee\":

$(\'#btnEmployee\').attr(\'src\',\'assets/img/employee.png\');

$(\'#btnHome\').attr(\'src\',\'assets/img/home-hover.png\');

break;

default:

$(\'#btnEmployee\').attr(\'src\',\'assets/img/employee-hover.png\');

$(\'#btnHome\').attr(\'src\',\'assets/img/home.png\');

}

}

function getParameterByName(name) {

name = name.replace(/[\\[]/, \"\\\\\\[\").replace(/[\\]]/, \"\\\\\\]\");

var regex = new RegExp(\"[\\\\?&]\" + name + \"=([^]*)\"),

results = regex.exec(location.search);

return results == null ? \"\" : decodeURIComponent(results[1].replace(/\\+/g, \" \"));

}

function clearModal(){

$(\"#myModalLabel\").html(\'\');

$(\"#myModalLabel\").html(\'New Record\');

$(\"#lastname\").val(\'\');

$(\"#firstname\").val(\'\');

$(\"#email\").val(\'\');

$(\"#id\").val(0);

}

function ajaxRequest(url, type, data, action){

var jsonData = \"\";

$.ajax({

url: url, //\'function.php?event=update\'

data: data, //\'code=masterpogi&name=masterpogitalagalang&id=21\',

dataType: \'json\',

type: type, //\'POST\',

success: function(result) {

switch (action){

case \'save\':

if (result.success == true){

alert(result.message);

location.reload();

}

break;

case \'delete\':

if (result.success == true){

alert(result.message);

location.reload();

}

break;

case \'edit\':

$(\"#myModalLabel\").html(\'\');

$(\"#myModalLabel\").html(\'Update Record\');

$(\"#lastname\").val(result.lastname);

$(\"#firstname\").val(result.firstname);

$(\"#email\").val(result.email);

$(\"#id\").val(result.id);

loadModal2();

break;

case \'update\':

if (result.success == true){

alert(result.message);

location.reload();

}

break;

default:

}

},

error: function () {

}

});

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值