简体   繁体   English

如何区分jquery中表单提交前的两次特定点击?

[英]How to differentiate between two specific click before form submission in jquery?

I have two events, first one is for adding new element and second one is for editing the elements .我有两个事件,第一个是添加新元素,第二个是编辑元素

Without submitting the form, I'm not sure how can I differentiate between them.如果不提交表格,我不确定如何区分它们。 I need the previous events so that I can use them on submit event.我需要以前的事件,以便我可以在提交事件中使用它们。 As, there are some conditions which are same for the add and edit , so I didn't keep them on separate file.由于addedit有一些相同的条件,所以我没有将它们保存在单独的文件中。 But for some conditions, I've to specify if those work for "add" event or "edit" event which I can't seem to find any way.但是对于某些情况,我必须指定这些是否适用于我似乎找不到任何方法的“添加”事件或“编辑”事件。

This is my code:这是我的代码:

$('.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
});

Use a variable, eg.使用变量,例如。 action and set different values in different click listeners: action并在不同的点击监听器中设置不同的值:

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();
}); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM