One button firing another buttons click event

前端 未结 3 1354
面向向阳花
面向向阳花 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:10
    <input type="button" id="primaryButton" onclick="ExistingLogic()" />
    <input type="button" id="secondaryButton"/>
    
    $('#secondaryButton').click(function(){
        $("#primaryButton").click();
    })
    
    0 讨论(0)
  • 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

    <input type="button" id="primaryButton" />
    <input type="button" id="secondaryButton"/>
    

    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);
    
    0 讨论(0)
  • 2020-12-08 15:25

    I'm only familiar with ASP.net and C# buttons, but using C# you could wire two different buttons to the same click event handler. You could also do it client side by triggering the primary buttons click event with your secondary button. Here's a VERY simple example:

    HTML

    <input type="button" id="primaryButton" onclick="ExistingLogic()" />
    <input type="button" 
           id="secondaryButton" 
           onclick="document.getElementById('primaryButton').click()" />
    
    0 讨论(0)
提交回复
热议问题