<script type="text/javaScript">
$(document).ready(function(){
// 등록버튼 클릭
$('#save').on("click", function(){
if(!validation()) { // 유효성 체크
return false;
}
if(confirm("Would you like to save?")) {
save();
}
});
// 수정버튼 클릭
$('#edit').on("click", function(){
if(!validation()) { // 유효성 체크
return false;
}
//
'''''''''' (중략)'''''''''''
//
if(confirm("Would you like to save with your changes?")) {
update();
}
});
function save() {
$.ajax({
type: "post",
dataType: "json",
data: $("#form").serialize(),
url: "<c:url value='/apply/0' />",
success: function(data) {
if(data.result == 'ok') {
alert("saved successfully");
document.location.href="/listApply";
} else {
alert("error has occurred");
}
},
error: function(error, result){
alert("error has occurred");
},
fail: function(failerr){
alert("fail to save a form");
}
});
}
// update
function update() {
$.ajax({
type: "post",
dataType: "json",
data: $("#form").serialize(),
url: "<c:url value='/apply/${appList.id}' />",
success: function(data) {
if(data.result == 'ok') {
alert("updated successfully");
document.location.reload();
} else {
alert("error has occurred");
}
},
error: function(error, result){
alert("error has occurred");
},
fail: function(failerr){
alert("fail to update a form");
}
});
}
<form id="form" name="form" method="post">
<input type="text" name="id" id="id" value="${appList.id}">
<input type="text" name="cont" id="cont" value="${appList.cont}">
</form>
<!-- button area -->
<div>
<c:choose>
<c:when test="${empty appList}
<button type="submit" id="save">Save</button>
</c:when>
<c:otherwise>
<button type="submit" id="edit">Edit</button>
</c:otherwise>
</c:choose>
<button type="button" onclick="fnList()">List</button>
</div>
<c:choose> 태그를 사용하면 페이지 마다 하나의 submit만 작동할 줄 알았는데 아니였음..ㅠㅠ
(로직을 잘 모르니까 나왔던 실수, 오류 였던 것 같음!)
<script type="text/javaScript">
$(document).ready(function(){
let listSize = ${sellDtl.size() };
/* controller에서 넘어온 list에
내용이 있다..! -> 상세보기/수정화면임
없다..! -> 등록하기
*/
if(listSize > 0) {
$("#save").text("Edit");
}
// 버튼에 적힌 이름이 edit인지 save인지
let btnType = $("#save").text();
$('#save').on("click", function(){
if(!validation()) {
return false;
}
// 저장 save
if(btnType == "Save") {
console.log("new-submit");
if(confirm("Would you like to save?")) {
save();
}
// 수정 edit
} else if(btnType == "Edit") {
console.log("edit");
if(confirm("Would you like to save your changes?")) {
update();
}
});
})
//
'''' function save/update는 기존 코드와동일하므로 생략 ! '''''''
//
<form id="form" name="form" method="post" onsubmit="return false;">
<input type="text" name="id" id="id" value="${appList.id}">
<input type="text" name="cont" id="cont" value="${appList.cont}">
</form>
<!-- button area -->
<div>
<button type="submit" id="save">Save</button>
<button type="button" onclick="fnList()">List</button>
</div>