One button firing another buttons click event

前端 未结 3 1357
面向向阳花
面向向阳花 2020-12-08 14:46

I\'d like two submit buttons on a form i have my team building, one above the fold, and one below. I\'m getting complaints from my tech team about adding it because it requi

3条回答
  •  遥遥无期
    2020-12-08 15:21

    If you want to use vanillaJS to do this... here is a generic very long way (with functions for both to be clear what is happening).

    html

    
    
    

    script

    const primary = document.getElementById('primaryButton');
    const secondary = document.getElementById('secondaryButton');
    
    function somePrimaryAction(e){
      e.preventDefault();
      console.log('you clicked the primary button');
    }
    
    function clickPrimaryButton(e){
      e.preventDefault();
      console.log('you clicked the secondary button');
      primary.click();
    }
    
    primary.addEventListener("click", somePrimaryAction, false);
    secondary.addEventListener("click", clickPrimaryButton, false);
    

提交回复
热议问题