简体   繁体   English

单击事件未触发选项卡中的动态添加按钮

[英]on click event not firing for dynamically added button inside tab

I have a page with 2 tabs. 我有一个带有2个标签的页面。 Each tab is loaded with its own DataTable and toolbar controls. 每个选项卡均加载有其自己的DataTable和工具栏控件。 Depending on which tab is active, I dynamically add the appropriate toolbar(with custom buttons). 根据活动的选项卡,我动态添加适当的工具栏(带有自定义按钮)。 However the onclick event does not fire for the dynamically added elements inside the tabs. 但是,对于选项卡中动态添加的元素,不会触发onclick事件。 Below is the code I am using to add the controls to different tabs: 以下是我用来将控件添加到不同选项卡的代码:

 $("#tabs").tabs( {
        active: 0,//Tab no.2 "Sha-2" by default active on page load,
        "activate": function(event, ui) {
            var table = $.fn.dataTable.fnTables(true);

            if ( table.length > 0 ) {

                $(table).dataTable().fnAdjustColumnSizing();
                var active = $( "#tabs" ).tabs( "option", "active" );
                if(active == 0){ //add toolbar to tab index = 1
                    $("div.toolbar").html('');

                }
                if(active == 1){ //add toolbar to tab index = 1


                    $("div.toolbar").html('<a id= "add-vendor"class="ui-button ui-widget ui-corner-all">ADD VENDOR</a><a id= "create-purchase_order"class="ui-button ui-widget ui-corner-all">CREATE PO</a>');

                    $( "#add-vendor" ).button().on( "click", function() {
                    alert('hello');
                    });

                }
            }
        }
    } );

The code is inside a document ready function.Could someone help me to catch the onclick event for the button? 该代码位于文档就绪函数内。有人可以帮助我捕获按钮的onclick事件吗?

It does not work because you are converting anchor tag to a button. 它不起作用,因为您正在将锚标记转换为按钮。 If you remove the button method then it works correctly : 如果删除按钮方法,则它可以正常工作:

 $("div.toolbar").html('<a id= "add-vendor"class="ui-button ui-widget ui-corner-all">ADD VENDOR</a><a id= "create-purchase_order"class="ui-button ui-widget ui-corner-all">CREATE PO</a>');

  $( "#add-vendor" ).on( "click", function() {
      alert('hello');
  });

If you want to use button then create a button tag instead of anchor tag. 如果要使用按钮,请创建一个按钮标签而不是锚标签。

$(document).on('click', "#add-vendor", function() {
           alert('hello');
   });

Will do the trick, delegate the event to the document for dynamically added elements. 可以做到这一点,将事件委托给文档以动态添加元素。

you need to delegate the dynamically added buttons: 您需要委派动态添加的按钮:

$(*ParentOfTheDynamicallyAddedButton*).delegate("DynamicallyAddedButton", "click", function(){
   *your function*
   }

You could say your browser is unaware of dynamically added elements, as they are not part of the script. 您可能会说您的浏览器没有意识到动态添加的元素,因为它们不是脚本的一部分。 they only exist in the DOM and therefore have no script attached to them. 它们仅存在于DOM中,因此没有附加脚本。 but don't quote me on that, im a noob. 但请不要在这方面引用我,我是菜鸟。

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

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