简体   繁体   中英

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" . To make it invisible to the user, you could use inline styles like this: style="display: none;" .

As a result, your button looks like this: <input id="submit" style="display: none" type="button" value="Reverse Geocode">


Now, to click it, simply call the click() method:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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