Javascript 在javascript中模拟按钮点击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14569320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simulating Button click in javascript
提问by ouyadi
So what i want to do is when i click on a button, it will pass this click event to another element in webpage, or you can say it will create a new click event in another element. Below is my code, it does not work, please let me know what is wrong with it, it looks make sense...
所以我想要做的是当我点击一个按钮时,它会将这个点击事件传递给网页中的另一个元素,或者你可以说它会在另一个元素中创建一个新的点击事件。下面是我的代码,它不起作用,请让我知道它有什么问题,它看起来很有意义......
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
$(function() {
$( "#datepicker" ).datepicker();
});
</script>
</head>
<body>
<p>Date: <input type="text" id="datepicker" onClick=alert("error") /></p>
<button type="button" value="submit" onClick="document.getElementById("datepicker").click()">submit </button>
</body>
</html>
回答by Matt Zeunert
Since you are using jQuery you can use this onClick handler which calls click:
由于您使用的是 jQuery,您可以使用这个 onClick 处理程序,它调用click:
$("#datepicker").click()
This is the same as $("#datepicker").trigger("click").
这与$("#datepicker").trigger("click").
For a jQuery-free version check out this answer on SO.
对于无 jQuery 版本,请查看SO 上的这个答案。
回答by sdespont
回答by weir
The smallest change to fix this would be to change
解决此问题的最小更改是更改
onClick="document.getElementById("datepicker").click()">
to
到
onClick="$('#datepicker').click()">
click()is a jQuery method. Also, you had a collision between the double-quotes used for the HTML element attribute and those use for the JavaScript function argument.
click()是一个 jQuery 方法。此外,您在用于 HTML 元素属性的双引号和用于 JavaScript 函数参数的双引号之间发生了冲突。
回答by Chase
The reason your code isn't working the way you would expect is because this line:
您的代码没有按您预期的方式工作的原因是因为这一行:
<button type="button" value="submit" onClick="document.getElementById("datepicker").click()">submit </button>
should be changed to:
应改为:
<button type="button" value="submit" onClick="document.getElementById('datepicker').focus()">submit </button>
There are two things to notice here:
这里有两点需要注意:
1: The "s around datepickerhave been changed to 's so that they do not interfere with the quotes surrounding the onclickevent.
1:"周围的sdatepicker已更改为's,这样它们就不会干扰onclick事件周围的引号。
2: The click()has been changed to focus()to activate the datepicker calendar. When the button is pressed.
2:click()已更改focus()为激活日期选择器日历。当按钮被按下时。
Now, this fixes your issue...but I do agree with the other posts that using jQuery to access the DOM element and trigger the event is the better way to go. Since you're already doing this for the jQuery datapicker plugin via <script src="http://code.jquery.com/jquery-1.8.3.js"></script>, this should not be a problem.
现在,这解决了您的问题……但我同意其他帖子,即使用 jQuery 访问 DOM 元素并触发事件是更好的方法。由于您已经通过 为 jQuery datapicker 插件执行了此操作<script src="http://code.jquery.com/jquery-1.8.3.js"></script>,因此这应该不是问题。
Inline events are not recommended.
不推荐内联事件。
回答by Webapper
Or you can use what JQuery alreay made for you:
或者您可以使用 JQuery 为您制作的内容:
http://jqueryui.com/datepicker/#icon-trigger
http://jqueryui.com/datepicker/#icon-trigger
It's what you are trying to achieve isn't it?
这就是你想要达到的目标,不是吗?
回答by Z3r0n3
try this
尝试这个
document.getElementById("datapicker").addEventListener("submit", function())
回答by subindas pm
Use this
用这个
jQuery("input.second").trigger("click");

