简体   繁体   English

载入页面后触发点击

[英]Trigger click after load page

I'm working with a wordpress theme and I need to activate a button when the page loads, but none of the following has worked for me (my button has the ID "reset"): 我正在使用wordpress主题,并且在页面加载时需要激活一个按钮,但是以下任何一项都不适合我(我的按钮的ID为“重置”):

$(document).ready(function() {
   $("#reset")[0].click();
}); 

-- -

$(document).ready(function(){
   $("#reset").click();
})

-- -

$(window).load(function(){
      $('#reset').click();
});

I put the code in the header or in the page where i need to activate the button, but does not work. 我将代码放在需要激活该按钮的标题或页面中,但不起作用。

Thanks! 谢谢!

JavaScript does not allow seamless programmatic triggering of an actual click event. JavaScript不允许无缝编程触发实际的点击事件。

What you could do is 你能做的是

  1. declare the click callback as a separate, named function 将click回调声明为单独的命名函数

eg 例如

function myClickCallback(e) {
  // Do stuff here
}
  1. Set this as the click callback of your button (eg $('#reset').on('click', myClickCallback) ). 将此设置为按钮的单击回调(例如$('#reset').on('click', myClickCallback) )。

  2. Invoke the callback when the page loads (eg $(document).ready(myClickCallback); ) 页面加载时调用回调(例如$(document).ready(myClickCallback);

I am not sure why you 'd want this functionality, since it sounds weird. 我不确定为什么要使用此功能,因为它听起来很奇怪。 From reading your description, by "activating", you could also mean enabling the button. 通过阅读您的描述,通过“激活”,您还可能意味着启用该按钮。 To do that you should do something like the following 为此,您应该执行以下操作

$(document).on('ready', function (e){
    $('#reset').removeAttr('disabled');
});

You may need to refer to it using jQuery instead of $ : 您可能需要使用jQuery而不是$来引用它:

The jQuery library included with WordPress is set to the noConflict() mode... WordPress随附的jQuery库设置为noConflict()模式...

https://codex.wordpress.org/Function_Reference/wp_enqueue_script#jQuery_noConflict_Wrappers https://codex.wordpress.org/Function_Reference/wp_enqueue_script#jQuery_noConflict_Wrappers

$(document).ready(function(){
 $("#reset").trigger('click');
});

I give you example here 我给你例子在这里

$(document).ready(function(){
   $("#reset").click();
})

the code above should work. 上面的代码应该可以工作。

Use the function Trigger with the event click 在事件单击时使用触发功能

$(document).ready(function(){
 $("#reset").trigger('click');
});

This what works for me: 这对我有用:

$(function () {
        $('#btnUpdatePosition').click();  
});

On the button event, the JQuery binding event doesn't work: 在按钮事件上,JQuery绑定事件不起作用:

$('#btnUpdatePosition').click(function () {
            alert('test again');
        });

But it works when I added the event on attribute declaration: 但是当我在属性声明中添加事件时,它可以工作:

<input type="button" value="Update" id="btnUpdatePosition" onclick="alert('Click has been called')" />

You can also call if you have a function on element: 如果元素上具有函数,也可以调用:

<input type="button" value="Update" id="btnUpdatePosition" onclick="fncShow();" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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