简体   繁体   English

一个按钮触发另一个按钮单击事件

[英]One button firing another buttons click event

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 requires some server side coding to make sure the user doesn't click it more than once. 我收到了我的技术团队关于添加它的投诉,因为它需要一些服务器端编码以确保用户不会多次点击它。 Apparently they have it one button, but to add that validation to two would be a problem. 显然他们有一个按钮,但要将验证添加到两个将是一个问题。

Can you not just call the button the same thing, with the same ID and wouldn't the form treat it as one button? 你能不能只用相同的ID调用按钮,并且不会将表单视为一个按钮?

Another option I thought would be for new button to fire a click even on the other button. 我认为另一个选项是新按钮甚至在另一个按钮上点击一下。 Then they still have one click even for the form, but I get my two buttons. 然后他们仍然只有一个单击即使表单,但我得到了我的两个按钮。 How would I write that? 我该怎么写呢?

Thanks, Adma 谢谢,阿德玛

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. 我只熟悉ASP.net和C#按钮,但是使用C#你可以将两个不同的按钮连接到同一个click事件处理程序。 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 HTML

<input type="button" id="primaryButton" onclick="ExistingLogic()" />
<input type="button" 
       id="secondaryButton" 
       onclick="document.getElementById('primaryButton').click()" />
<input type="button" id="primaryButton" onclick="ExistingLogic()" />
<input type="button" id="secondaryButton"/>

$('#secondaryButton').click(function(){
    $("#primaryButton").click();
})

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). 如果你想使用vanillaJS来做这个......这是一个非常长的通用方法(两个函数都可以清楚地说明发生了什么)。

html 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);

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

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