页面内容foreach时,ajax增删改

var main = function() {
var validator;
var initPage = function() {
validator = $("#modify").bind("invalid-form.validate", function() {
}).validate({
// debug: true,
errorElement : "em",
errorPlacement : function(error, element) {
showValidateMessage(element, error[0].innerText);
},
rules : {
title : {
required : true,
maxlength : 60,
minlength : 1,
allchar : true
},
context : {
required : true,
maxlength : 500,
minlength : 1,
allchar : true
}
},
messages : {
title : {
required : $("#CCMP_W2001").val(),
maxlength : $("#CCMP_W2002").val(),
minlength : $("#CCMP_W2002").val(),
allchar : $("#CCMP_W2003").val()
},
context : {
required : $("#CCMP_W2001").val(),
maxlength : $("#CCMP_W2002").val(),
minlength : $("#CCMP_W2002").val(),
allchar : $("#CCMP_W2003").val()
}
}
});
}
/**
* フォームをクリアする

* @returns
*/
function clearForm() {
validator.resetForm();
$(".form-control").removeClass("error");
$(".callout").hide();
showMessage("alert-modal", "", "");
}

var showForm = function() {

//增加按钮只有一个,在foreach外,点击时‘ <a href="#" class="new showForm" data-toggle="modal" data-target="#confirmModal" title="New">’

//data-target="#confirmModal" 使模态框弹出

$(".showForm").click(function() {

clearForm();

//模态框清空数据

$("#newsSeq").val("");
$("#paymentType").val("");
$("#savebtn").html("Save");
$(".modifyMode").hide();
focusoutCheck_allChar("title", 60, "CCMP_W2003");
focusoutCheck_allChar("context", 500, "CCMP_W2003");
});

//点击foreach修改时
$(".modifyModal").click(function() {
clearForm();
$("#savebtn").html("Save changes");

var newsSeq = $(this).attr("param");

//根据<a href="#" data-toggle="modal" class="modifyModal"  title="Edit" param="${news.newsSeq}"/>

//ajax后台取得数据

$.ajax({
url : $("#domain").val()+ '/home/ajax_getNewsByNewsSeq/'+ newsSeq,
dataType : 'JSON',
success : function(data, textStatus,jqXHR) {
if (jqXHR.responseJSON != null) {
if (jqXHR.responseJSON.redirect == true) {
location.href = jqXHR.responseJSON.redirectUrl;
return;
}
}
// Jsonを変更する
var result = eval(data);
if (result != null) {
$("#title").val(result["title"]);
$("#context").val(result["context"]);
$("#newsSeq").val(result["newsSeq"]);
$("#version").val(result["version"]);
}
},
error : function(XMLHttpRequest,textStatus, errorThrown) {
window.location.href = $("#domain").val()+ "/error/ccmp_error";
}

});

//手动显示模态框

$("#confirmModal").modal('show');

//共通验证方法调用

focusoutCheck_allChar("title", 60, "CCMP_W2003");
focusoutCheck_allChar("context", 500, "CCMP_W2003");

});

//删除按钮点击事件

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

//修改方法一样取得主键

var newsSeq = $(this).attr("param");

//给隐藏输入框赋值

$('#deleteNewsSeq').val(newsSeq);
$("#modal_delete").modal('show');
});


}
/**
* 追加または更新
*/
// save按钮
var saveClick = function() {
$("#savebtn").unbind("click").bind("click", saveEvent);
}
function saveEvent() {
clearForm();
if ($("#modify").valid()) {
var header = $("meta[name='_csrf_header']").attr("content");
var token = $("meta[name='_csrf']").attr("content");
$("#savebtn").attr("disabled", true);
if ($("#newsSeq").val() != "") {
$.ajax({
url : $("#domain").val() + "/home/ajax_update/",
type : "post",
beforeSend : function(xhr){ 
xhr.setRequestHeader(header, token);

},

//表单的数据,后台接收表单类

data : $("#modify").serialize(),
success : function(result, jqXHR) {
if (jqXHR.responseJSON != null) {
if (jqXHR.responseJSON.redirect == true) {
location.href = jqXHR.responseJSON.redirectUrl;
return;
}
}


// Jsonを変更する
var str = eval(result);
if (str != null) {
if (str["result"] == "OK") {
// showMessage("alert-body",

// str["message"].split("#"), str["type"]);

//成功重新刷新页面

location.href = "init";
}

if (str["result"] == "ERROR") {

//调用共通方法显示错误信息

showMessage("alert-modal", str["message"].split("#"), str["type"]);
}
}
$("#savebtn").removeAttr("disabled");
},

error : function(XMLHttpRequest, textStatus, errorThrown) {

//错误画面

window.location.href = $("#domain").val() + "/error/ccmp_error";
}
});
} else {
$.ajax({
url : $("#domain").val() + "/home/ajax_save/",
type : "post",
beforeSend : function(xhr) {
xhr.setRequestHeader(header, token);
},
data : $("#modify").serialize(),
success : function(result, jqXHR) {
if (jqXHR.responseJSON != null) {
if (jqXHR.responseJSON.redirect == true) {
location.href = jqXHR.responseJSON.redirectUrl;
return;
}
}


// Jsonを変更する
var str = eval(result);


if (str != null) {
if (str["result"] == "OK") {
location.href = "init";
} else {
showMessage("alert-modal", str["message"].split("#"), str["type"]);
}
}
$("#savebtn").removeAttr("disabled");
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
window.location.href = $("#domain").val() + "/error/ccmp_error";
}
});


}


}
}


/**
* 削除
*/
var del = function() {
$(".btn-danger").unbind("click").bind("click", delEvent);
};


function delEvent() {

$(".btn-danger").attr("disabled", true);

//取得点击删除按钮时给隐藏输入框赋的值

var newsSeq = $('#deleteNewsSeq').val();
var header = $("meta[name='_csrf_header']").attr("content");
var token = $("meta[name='_csrf']").attr("content");
$.ajax({
url : $("#domain").val() + "/home/ajax_delete/" + newsSeq,
type : "post",
beforeSend : function(xhr) {
xhr.setRequestHeader(header, token);
},
success : function(result, jqXHR) {
if (jqXHR.responseJSON != null) {
if (jqXHR.responseJSON.redirect == true) {
location.href = jqXHR.responseJSON.redirectUrl;
return;
}
}


// Jsonを変更する
var str = eval(result);


if (str != null) {
if (str["result"] == "OK") {
location.href = "init";
} else {
showMessage("alert-modal", str["message"].split("#"), str["type"]);
}
}
$(".btn-danger").removeAttr("disabled");
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
window.location.href = $("#domain").val() + "/error/ccmp_error";
}
});
}


return {

init : function() {

//弹出框事件

showForm();

//内容验证

initPage();

//保存/修改按钮点击事件

saveClick();

//删除事件

del();
}
}
}().init();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值