我有兩個事件,第一個事件用于添加新元素,第二個事件用于編輯元素。如果不提交表格,我不確定如何區(qū)分它們。我需要以前的事件,以便我可以在提交事件中使用它們。由于添加和編輯有一些相同的條件,所以我沒有將它們保存在單獨的文件中。但對于某些條件,我必須指定這些是否適用于“添加”事件或“編輯”事件,我似乎找不到任何方法。這是我的代碼:$('.addButton').on('click',function(){ //console.log('add event'); $('#forTest').modal('toggle');}); //after trigerring this will go to the below submit code$('#submitForm').on('click',function () { // some conditions /* where condition is same except the error message are different like for add : this message : values are empty edit: this message : same values are there */ $('#test_form').submit();}); $('.edit-button'.on('click', function(){ $('#forTest').modal('toggle'); //edit button which will trigger to upper submit form});
1 回答

泛舟湖上清波郎朗
TA貢獻1818條經(jīng)驗 獲得超3個贊
使用變量,例如。action并在不同的點擊監(jiān)聽器中設置不同的值:
let action;
$('.addButton').on('click',function(){
action = "add";
$('#forTest').modal('toggle');
});
$('.edit-button'.on('click', function(){
action = "edit";
$('#forTest').modal('toggle'); //edit button which will trigger to upper submit form
});
$('#submitForm').on('click',function () {
if(action === "edit"){ /* Edit code */}
else{ /* Add code */ }
$('#test_form').submit();
});
- 1 回答
- 0 關注
- 143 瀏覽
添加回答
舉報
0/150
提交
取消