简体   繁体   English

当用户点击另一个显示的输入标签时,如何自动点击隐藏的子输入标签?

[英]How can automatically click on an hidden submint input tag when the user click on another shown input tag?

I am absolutely new in JavaScript and I have the following problem. 我是JavaScript的新手,我有以下问题。

For some reason in a web page I have 2 input tag having type=submit , these: 出于某种原因,在网页中我有2个input标签,其type=submit ,这些:

<input type="submit" onclick="return myTestFunction();" value="Submit" id="submitEventButton">

<input id="submitButton" type="submit" style="display:none" onclick="return validateForm();" value="Submit" name="action:projectCreationAction">

As you can see the second one is hidden by the use of style="display:none" 正如你所看到的那样,使用style="display:none"隐藏了第二个

Clicking on the first one I can enter into the myTestFunction() JavaScript method (that actually simply perform an alert). 单击第一个我可以进入myTestFunction() JavaScript方法(实际上只是执行警报)。

I need to do the following thing: when the user click on the first button (the one having id="submitEventButton" ) automatically the hidden second button have to be clicked. 我需要做以下事情:当用户自动点击第一个按钮(具有id="submitEventButton"按钮)时,必须单击隐藏的第二个按钮。

How can I do it? 我该怎么做?

Thanks! 谢谢!

You could place the following inside your myTestFunction , after the alert: 您可以在警报之后将以下内容放在myTestFunction

$('#submitButton').trigger("click");

Below is snippet you can run to check this out: 以下是您可以运行以检查此内容的代码段:

 function myTestFunction(){ alert('hi from submit'); $("#submitButton").trigger("click") ; } function validateForm(){ alert('hi from validate form'); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="submit" onclick="return myTestFunction();" value="Submit" id="submitEventButton"> <input id="submitButton" type="submit" style="display:none" onclick="return validateForm();" value="Submit" name="action:projectCreationAction"> 

Instead of simulating a click or even having a second hidden submit button at all, you can just call the function that the second button is hitting from the first function: 您可以只调用第二个按钮从第一个函数点击的函数,而不是模拟单击或甚至根本没有第二个隐藏的提交按钮:

function myTestFunction() {
    // code here with alert
    validateForm();
}

function validateForm() {
    // code here
}

functions calling other functions is better practice than buttons triggering other click events. 调用其他函数的函数比触发其他单击事件的按钮更好。

this way is also pure javascript (no jquery) since i dont think with the implementation of your input code you are currently using jquery. 这种方式也是纯javascript(没有jquery),因为我不认为你输入代码的实现,你目前正在使用jquery。

Your question is not so clear about it, but it seems like you don't really need the second button at least for what you're describing. 你的问题并不是那么清楚,但你似乎并不需要第二个按钮,至少你所描述的是什么。 What you can do to achieve what you're after is add a call to validateForm() in the body of myTestFunction . 你可以做些什么来实现你所追求的是在myTestFunction的主体中添加对validateForm()myTestFunction

If you do not want to change the function for some reason, you can also change the first button like so: 如果由于某种原因不想更改功能,也可以像这样更改第一个按钮:

<input type="submit" onclick="myTestFunction();validateForm();" value="Submit" id="submitEventButton">

It ain't pretty, and note that i assume here that myTestFunction doesn't return something important otherwise you can't discard the return as i suggested here, but other than that it would probably do the trick. 它不漂亮,请注意我在这里假设myTestFunction没有返回重要的东西,否则你不能丢弃我在这里建议的return ,但除此之外它可能会做的伎俩。

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

相关问题 单击跨度时,如何获取由跨度标签括起来的文本并将其保存为隐藏的输入值? - How can get the text enclosed by a span tag and save it to a hidden input value when I click the span? 如何检查<input>标签是否点击? - How to check <input> tag is click or not? 当我单击 td 标签(javascript + html)时,我无法将输入标签添加到 td 标签中 - i can't add input tag into td tag when i click td tag (javascript + html) 当我单击 jquery 中的 i 标记时,如何定位最近的输入[类型] - How can I target the nearest input[type] when I click the i tag in jquery 将点击输入更改为选项标签 - changing input on click to option tag 在锚标签单击时更新输入标签值 - Update input tag value on anchor tag click 当用户单击按钮时,如何使其自动在文本字段中输入特定的关键字? - How can I make it automatically input particular keyword into text field when a user click a button? 如何使用Google跟踪代码管理器Click事件获取隐藏输入的属性 - How to grab a hidden input's attributes using Google Tag Manager Click Event 单击选择标记时,为什么IE不会取消选择输入中的文本 - When click select tag why IE does not deselect text in input 当我点击一个按钮时,我的输入标签变为空 - My input tag becomes empty when I click on a button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM