为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

湖上湖 2023-12-25 16:19:18
我有两个事件,第一个事件用于添加新元素,第二个事件用于编辑元素。如果不提交表格,我不确定如何区分它们。我需要以前的事件,以便我可以在提交事件中使用它们。由于添加和编辑有一些相同的条件,所以我没有将它们保存在单独的文件中。但对于某些条件,我必须指定这些是否适用于“添加”事件或“编辑”事件,我似乎找不到任何方法。这是我的代码:$('.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条经验 获得超3个赞

使用变量,例如。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();

}); 


查看完整回答
反对 回复 2023-12-25
  • 1 回答
  • 0 关注
  • 43 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信