Jquery prepend click handler

孤人 提交于 2019-12-01 19:49:38
        $(":button").each(function() {

            // your button
            var btn = $(this); 

            // original click handler
            var clickhandler = btn.attr("onclick");
            btn.attr("onclick", "return false;");

            // new click handler
            btn.click(function() {

                if ($("#chkbox").attr("checked") == true) {

                    // TODO: show your help
                    // TODO: catch event

                }
                else {

                    clickhandler();

                }
            });
        });

I think you want to play around with bind/unbind in this case - see http://docs.jquery.com/Events/unbind#typefn

when selecting the checkbox you can bind new click events on your buttons - and unbind them later on.

You will probably also have to work with Event.preventDefault (http://docs.jquery.com/Events/jQuery.Event) in order to suppress the default click behavior if you dont want it

You would use something the likes of:

$("a").bind("click", function(event){
 if ($("#helpcheckbox").attr('checked')
 {
  event.preventDefault();
  // do something
 }
});

Without more code it's hard to be more exact.

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