[英]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.