简体   繁体   English

如何在html或javascript中默认按下按钮

[英]how to make a button pressed by default in html or javascript

在那里,我有一个隐藏在用户之外的按钮,但希望它在默认情况下被点击,就像复选框一样,如果你想在默认情况下检查它,你添加了checked属性是否有任何办法你可以做同样的事情用一个这里的按钮是我的代码

<input id="submit" type="hidden" value="Reverse Geocode" autofocus> 

You can do as following: 您可以执行以下操作:

<script type="text/javascript">
document.getElementById("submit").click();
</script>

At first, your button is not a button. 首先,您的按钮不是按钮。 It's aa hidden field. 这是一个隐藏的领域。

In order to make it a button, change type="hidden" to type="button" . 为了使它成为一个按钮,将type="hidden"更改为type="button" To make it invisible to the user, you could use inline styles like this: style="display: none;" 要使用户不可见,您可以使用如下内联样式: style="display: none;" .

As a result, your button looks like this: <input id="submit" style="display: none" type="button" value="Reverse Geocode"> 因此,您的按钮如下所示: <input id="submit" style="display: none" type="button" value="Reverse Geocode">


Now, to click it, simply call the click() method: 现在,要单击它,只需调用click()方法:

document.getElementById('submit').click();

May be you can do the following: 可能你可以做到以下几点:

 document.getElementById('chkTest').addEventListener('click', function(){ if(this.checked) document.getElementById('submit').click(); }); document.getElementById('submit').addEventListener('click', function(){ alert('button clicked'); }); 
 <input id="submit" type="hidden" value="Reverse Geocode" autofocus /> <input type="checkbox" id="chkTest" /> Check To Click The Button 

Trigger click event on the button as soon as document is ready.You have to write the click event as shown below. 文档准备就绪后立即触发按钮上的单击事件。您必须编写单击事件,如下所示。

$(document).ready(function(){

    $("#yourButtonId")[0].click();

});  

Now i understand your question, You want default click in your submit button. 现在我理解你的问题,你想要默认点击你的提交按钮。 Try click event, It will trigger the submit. 尝试点击事件,它将触发提交。

<script>
   $('#submit').trigger('click');
</script>

In JavaScript 在JavaScript中

   document.getElementById("submit").click();

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

相关问题 按下按钮时如何使JavaScript工作? - How to make JavaScript work when button is pressed? 如果按下按钮,则Javascript自动刷新HTML - Javascript autorefresh HTML if button pressed 当按下ASP:Button时,如何使html元素包含文本? - How to make an html element contain text when an ASP:Button is pressed? 如何在 html 中制作一个一直按住直到我再次按下它的按钮? - How to make a button in html that keep pressed till i press it again? Javascript:按下按钮时如何进行淡入淡出转换? - Javascript: How to make fade in and out transition when button is pressed? 如何检查按钮是否被按下 javaScript - How to check if button was Pressed javaScript 我想在按下按钮时显示元素。 默认情况下显示该元素,如何使其默认不显示? - I want to make an element display when a button is pressed. by default the element is shown, how do I make it display none by default? 如何将javascript代码嵌入到html按钮中? - How to make javascript code to be embed into html button? 如何为此JavaScript / HTML代码添加一个按钮,以便我的代码仅在按下按钮后才会运行? - How do i add a button to this JavaScript/HTML code, so that my code only runs once the button is pressed? 如何使按钮保持按下状态直到再次按下 - how to make button stay pressed down until pressed again
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM