[英]jquery .click() does not work
on my php page I want to click a button on page load. 在我的php页面上,我想单击页面加载上的按钮。 I have tested it with jQuery and JS. 我已经用jQuery和JS测试了它。 While jQuery does not work, the JS works fine. 虽然jQuery不起作用,但JS可以正常工作。
Any idea why this is the case? 知道为什么会这样吗?
jQuery: jQuery的:
<script>
$(document).ready(function() {
$('#button_id').click();
$('#button_id').trigger('click');
});
</script>
JS: JS:
<script>
window.onload = function() {
document.getElementById('button_id').click();
};
</script>
The button I want to click has a data-filter
inside. 我要单击的按钮内部有一个data-filter
。 Might this be the problem? 这可能是问题吗?
<button id="button_id" data-filter=".parkett" class="sub">Button</button>
EDIT: 编辑:
No errors in the console, libary is added at the top. 控制台中没有错误,libary添加在顶部。
This is my console output if I log both buttons: 如果同时登录两个按钮,这是控制台输出:
$("document").ready(function() {
setTimeout(function() {
$("#button_id").trigger('click');
},10);
});
$('#button_id').click(); $('#button_id')。click();
You have defined no click handler - so nothing going to happen. 您没有定义点击处理程序-因此什么也不会发生。 You need to drop a function in there to use as the callback. 您需要在其中放置一个函数以用作回调。 Like this: 像这样:
$( "#button_id" ).click(function() {
$(this).trigger('click');
});
Sounds like you haven't included the jquery script in your head 听起来好像您的头中没有包含jquery脚本
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.