我有像这样的函数javascript代码
function insertData(link) {
$("#addGroup").validate({
ignore:[],
rules:{
mod_groupname: {
required: true,
minlength: 5,
maxlength: 30
},
mod_namegroup: {
required: true,
minlength: 7,
maxlength: 100
},
"hiddenRecaptcha": {
required: function() {
if(grecaptcha.getResponse() == '') {
return true;
} else {
return false;
}
}
}
},
errorPlacement: function(error, element)
{
if(element.closest('.form-group').find('label.error').length == 0){
error.css({
"color":"red",
"font-weight": "bolder"
}).insertBefore( element.closest('.form-group').find('.container_error'));
}
},
submitHandler: function(form) {
$form = $(form);
$.post(link,$form.serialize(),function(data){
var obj = jQuery.parseJSON(JSON.stringify(data));
if (jQuery.type(obj.message) == "object") {
$.each(data,function(index, value) {
if (jQuery.type(data[index]) == "object") {
var stringMessage = "";
$.each(data[index],function(urutan,isi) {
stringMessage += "- "+isi+"\n";
});
alert(stringMessage);
}
});
}
else if (jQuery.type(obj.message) == "string") {
alert(obj.message);
if (obj.code == 1) {
$("#modalAddGroup").modal('toggle');
var oTable = $("#view_group").dataTable();
oTable.fnDraw();
}
grecaptcha.reset();
$("#addGroup")[0].reset();
}
});
}
});
}
之后我在事件jQuery中使用函数
$("#modalShow").click(function() {
$("#hiddenCodeRole").remove();
grecaptcha.reset();
$("#addGroup")[0].reset();
$("#mod_codegroup").removeAttr("disabled");
$("#title-modal").text("Tambah grup hak akses");
$("#modalAddGroup").modal('toggle');
insertData("siteman/app/insert/insert_add_group.php");
});
$("#view_group").on("click",".btn-update",function() {
$.ajax({
url: "siteman/app/json/view_group_update.php",
dataType: 'json',
data: "rolecode="+$(this).attr("data"),
method: "POST",
success: function(result) {
grecaptcha.reset();
$("#addGroup")[0].reset();
var obj = JSON.parse(JSON.stringify(result));
if(obj.code == 1) {
$("#hiddenCodeRole").remove();
$("#mod_codegroup").val(obj.data.role_rolecode).addClass("disabled").attr("disabled","disabled");
$("#namegroup").val(obj.data.role_name);
$("#title-modal").text("Perbaharui grup hak akses");
$("#addGroup > .form-group:first").append("<input type='hidden' id='hiddenCodeRole' value='"+obj.data.role_rolecode+"' name='mod_rolecode' />");
$("#modalAddGroup").modal('toggle');
insertData("<?=BASEPATH?>siteman/app/change/process_edit_group.php");
} else {
alert('Ada terjadi error. silahkan anda mengulangi lagi!');
}
},
error: function() {
alert('Ada terjadi error. silahkan anda mengulang lagi!')
}
});
});
单击按钮类.btn-update并更新数据成功时的问题。我单击按钮modalShow并填写表单函数insertData(&#34; siteman / app / insert / insert_add_group.php&#34;)未调用,但此函数insertData(&#34; siteman / app / json / view_group_update.php& #34;)再次召集。
对不起我的英语不好:(。
答案 0 :(得分:0)
感谢stackoverflow,我得到了解决方案
$("#addGroup").validate({
ignore:[],
rules:{
mod_groupname: {
required: true,
minlength: 5,
maxlength: 30
},
mod_namegroup: {
required: true,
minlength: 7,
maxlength: 100
},
"hiddenRecaptcha": {
required: function() {
if(grecaptcha.getResponse() == '') {
return true;
} else {
return false;
}
}
}
},
errorPlacement: function(error, element)
{
if(element.closest('.form-group').find('label.error').length == 0){
error.css({
"color":"red",
"font-weight": "bolder"
}).insertBefore( element.closest('.form-group').find('.container_error'));
}
},
submitHandler: function(form,event) {
event.preventDefault();
$form = $(form);
var url = $form.attr("action");
$.post(url,$form.serialize(),function(data){
var obj = jQuery.parseJSON(JSON.stringify(data));
if (jQuery.type(obj.message) == "object") {
$.each(data,function(index, value) {
if (jQuery.type(data[index]) == "object") {
var stringMessage = "";
$.each(data[index],function(urutan,isi) {
stringMessage += "- "+isi+"\n";
});
alert(stringMessage);
}
});
}
else if (jQuery.type(obj.message) == "string") {
alert(obj.message);
if (obj.code == 1) {
$("#modalAddGroup").modal('toggle');
var oTable = $("#view_group").dataTable();
oTable.fnDraw();
}
grecaptcha.reset();
$("#addGroup")[0].reset();
}
});
}
});
$("#modalShow").click(function() {
$("#hiddenCodeRole").remove();
grecaptcha.reset();
$("#addGroup")[0].reset();
$("#mod_codegroup").removeAttr("disabled");
$("#title-modal").text("Tambah grup hak akses");
$("#modalAddGroup").modal('toggle');
$("#addGroup").attr("action","<?=BASEPATH?>siteman/app/insert/insert_add_group.php");
});
$("#view_group").on("click",".btn-update",function() {
$.ajax({
url: "<?=BASEPATH?>siteman/app/json/view_group_update.php",
dataType: 'json',
data: "rolecode="+$(this).attr("data"),
method: "POST",
success: function(result) {
grecaptcha.reset();
$("#addGroup")[0].reset();
var obj = JSON.parse(JSON.stringify(result));
if(obj.code == 1) {
$("#hiddenCodeRole").remove();
$("#mod_codegroup").val(obj.data.role_rolecode).addClass("disabled").attr("disabled","disabled");
$("#namegroup").val(obj.data.role_name);
$("#title-modal").text("Perbaharui grup hak akses");
$("#addGroup > .form-group:first").append("<input type='hidden' id='hiddenCodeRole' value='"+obj.data.role_rolecode+"' name='mod_rolecode' />");
$("#modalAddGroup").modal('toggle');
$("#addGroup").attr("action","<?=BASEPATH?>siteman/app/change/process_edit_group.php");
} else {
alert('Ada terjadi error. silahkan anda mengulangi lagi!');
}
},
error: function() {
alert('Ada terjadi error. silahkan anda mengulang lagi!')
}
});
});