Programatically moving an inline onclick function to a JQuery click() function

不羁的心 提交于 2019-12-11 23:05:33

问题


I'm hoping to be able to create a catch-all function which will allow me to dynamically set attributes on a legacy system. What I want to do is to create a JQuery click function which will disable the element and either submit the form or append the current onclick function to the JQuery click function. (It is assumed that the onclick code will handle the form)

So far I have

jQuery(element).click( function() {
onclick = (jQuery(this).attr('onclick') || jQuery(this).attr('onClick'));
if(form && !onclick) {
    jQuery(form).submit();
} else if (form && onclick) {
    jQuery(this).//how do I dynamically execute the onclick code using jquery?
}};

To the best of my knowledge this should be possible in javascript, however I can't find any similar threads.

EDIT

Thank you for the answers so far, however I need to clarify the problem further.

I need to be able to take the function currently in |tag "onclick=function()| and append that to the jQuery click function. This is necessary since I must unset the onclick attribute in order for the jQuery click event to fire, however I need to preserve the original functionality.


回答1:


Call the native javascript onclick event like that:

this.click();



回答2:


you can use:

$('element').click();

similar way of doing it for other events like change(), keyup(), etc..



来源:https://stackoverflow.com/questions/17522979/programatically-moving-an-inline-onclick-function-to-a-jquery-click-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!