[英]JQuery trigger click event not working
I'm having trouble getting the following to work: 我无法让以下工作:
$('#elem').trigger('click');
When I run it, it doesn't trigger a click but it doesn't show any error logs in the console either. 当我运行它时,它不会触发点击,但它也不会在控制台中显示任何错误日志。
I've also tried the following with no luck: 我也试过以下没有运气:
$('#elem')[0].click();
Any ideas as to what could be going wrong or a solution for this? 关于可能出现什么问题或解决方案的任何想法?
Update: Jquery is working and when I inspect the element, #elem does appear (it's an id for a button) 更新:Jquery正在工作,当我检查元素时,#elem确实出现(它是一个按钮的id)
HTML: HTML:
<a:button id="elem">My Button</Button>
JQuery: JQuery的:
P.when('A', 'jQuery').execute(function (A, $) {
//when this function is called, it should trigger a button click
triggerButtonClick = function() {
$('#elem').trigger('click');
};
});
Try it in document.ready function, then all dom loads when the document is ready.You can do something like this based on your requirement. 在document.ready函数中尝试它,然后在文档准备好时加载所有dom。你可以根据你的要求做这样的事情。
$( document ).ready(function() { $('#radio').click(function(){ $('#elem')[0].click(); }) });
i have tried this code and it works 我试过这个代码,它的工作原理
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"> </script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("chk1").click(function(){
$('#usr').trigger('click');
});
});
</script>
</head>
<body>
<form action="sample.php">
<button id="chk1">click</button>
<input class="form-control" type="submit" id="usr">
</form>
</body>
</html>
$(document).ready(function () { $('#elem').click(function () { alert('clicked'); $(this).css('color', 'red'); }); $('#elem').trigger('click'); });
<!DOCTYPE html> <html> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> <body> <div id="elem" >test</div> </body> </html>
this code works for me: 这段代码适合我:
$(window).load(function() {
$('#menu_toggle').trigger('click');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.