问题
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