【问题标题】:No event is happening upon click of a hyperlink which added under jquery tab单击在 jquery 选项卡下添加的超链接时不会发生任何事件
【发布时间】:2013-11-19 13:02:57
【问题描述】:

我已经创建了这样的 jquery 选项卡:

$(function() {
        $( "#tabContainer" ).tabs();
      });

在其中添加了一些 href 链接:

$(document).ready(function(){
    $("#tabContainer ul").append("<a href=# id='executePayLoad' class = 'buttonsExeSave'>Execute</a>");
        $("#tabContainer ul").append("<a href=# id='inputPayLoadSave' class = 'buttonsExeSave'>DownloadRequest</a>");
        $("#tabContainer ul").append("<a href=# id='expand' class = 'buttonsExeSave'>Expand</a>");
        $("#tabContainer ul").append("<a href=# id='collapse' class = 'buttonsExeSave'>Collapse</a>");

单击“executePayLoad”链接后,我想将新的 href 链接动态附加到现有选项卡之一,如下所示:

$("#executePayLoad").click(function(){
                if($('#outputPayLoadSave').length == 0)
                    $("#tabContainer ul").append("<a href=# id='outputPayLoadSave' class = 'buttonsExeSave'>DownloadResponse</a>");
.
.
.
.
});

点击“outputPayLoadSave”链接后,我想触发类似这样的事件:

$("#outputPayLoadSave").click(function(){
            alert("inside");
            GLR.messenger.show({msg:"Generating file...", mode:"loading"});
            var  xmlStr = xmlView.getXmlAsString();
              $('#hidden_form > input[name=xml_string]').val(xmlStr);
              $('#hidden_form > input[name=adapterName]').val(adapterValue);
              $('#hidden_form > input[name=mode]').val("response");
              $('#hidden_form').attr('action','xmlTreeView/Save.do');
              $('#hidden_form').submit();
              GLR.messenger.inform({msg:"Done.", mode:"success"});
        });

但是当我单击“outputPayLoadSave”链接时,上面写的事件没有发生,即使控件没有进入该事件函数。有人可以帮帮我吗?

【问题讨论】:

  • 当一组标记被动态添加到 DOM 中时,您应该使用委托函数。例如:live()(现已弃用)、.on() http://api.jquery.com/on/

标签: javascript jquery


【解决方案1】:

或者您可以先创建元素并将其隐藏。然后在单击第一个按钮时显示它。 JSfiddle:http://jsfiddle.net/u63CU/

$(document).ready(function(){
$("#tabContainer ul").append("<a href=# id='executePayLoad' class = 'buttonsExeSave'>Execute</a>");
    $("#tabContainer ul").append("<a href=# id='inputPayLoadSave' class = 'buttonsExeSave'>DownloadRequest</a>");
    $("#tabContainer ul").append("<a href=# id='expand' class = 'buttonsExeSave'>Expand</a>");
    $("#tabContainer ul").append("<a href=# id='collapse' class = 'buttonsExeSave'>Collapse</a>");
    $("#tabContainer ul").append("<a href=# id='outputPayLoadSave' class = 'buttonsExeSave'>DownloadResponse</a>");
    $("#outputPayLoadSave").hide();
});

$("#executePayLoad").click(function(){
    $("#outputPayLoadSave").show();
});

$("#outputPayLoadSave").click(function(){
    alert("inside");
});

【讨论】:

    【解决方案2】:

    您必须使用 .on 函数将处理程序与动态创建/添加到 DOM 中的元素挂钩,但是在查看您的代码时,您通常只是将单击事件与该锚元素挂钩。显然,它不会起作用。

    $(document.body).on("click","#outputPayLoadSave",function(){
    
    });
    

    您的代码的干净版本,

    $(document.body).on("click","#outputPayLoadSave",function(){
    
                  alert("inside");
    
                  GLR.messenger.show({msg:"Generating file...", mode:"loading"});
                  var  xmlStr = xmlView.getXmlAsString();
                  $('#hidden_form > input[name=xml_string]').val(xmlStr);
                  $('#hidden_form > input[name=adapterName]').val(adapterValue);
                  $('#hidden_form > input[name=mode]').val("response");
                  $('#hidden_form').attr('action','xmlTreeView/Save.do');
                  $('#hidden_form').submit();
                  GLR.messenger.inform({msg:"Done.", mode:"success"});
    
    });
    

    【讨论】:

    • 感谢您的回复。我使用了这种方法,它对我有用:$("#outputPayLoadSave").live('click',function(){
    • @sarfaraz 不要实时使用,它已被弃用。而是使用 .on。你试过我的代码了吗?
    【解决方案3】:

    试试这个

    $(document).on('click', '#executePayLoad", function(){ /*..yourCode...*/ });
    

    【讨论】:

    • 感谢 Majid,但我试过了,它节省了我的时间:$("#outputPayLoadSave").live('click',function(){
    • 但是你使用了一个不推荐使用的方法
    猜你喜欢
    • 1970-01-01
    • 2015-05-19
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多