简体   繁体   English

JavaScript中的递归过多

[英]Too much recursion in JavaScript

I have this JavaScript which opens new page: 我有这个JavaScript,可以打开新页面:

$(document).ready(function () {
    //$('a[id$="lnkHidden"]').trigger("click");    // Not sure if this is actually necessary

    $('table[id$="dataTable"]').find("tbody").on("click", "tr", function () {
        $(this).find('a[id$="lnkHidden"]').trigger("click");
    });
});

This is the button which is called by the JS script: 这是JS脚本调用的按钮:

<h:commandLink id="lnkHidden" action="#{bean.pageRedirect}" style="text-decoration:none; color:white; display:none">

</h:commandLink>

After I click on a table row I get this error message: 单击表格行后,出现以下错误消息:

too much recursion [Break On This Error] ...,c=l.length;c--;)(f=l[c])&&(v[d[c]]=!(y[d[c]]=f));if(i){if(o||e){if(o){for(l=‌​[],...

Can you help me to fix this? 你能帮我解决这个问题吗?

You can cut the infinite loop with those changes from your original code 您可以使用原始代码中的更改来切入无限循环

  • add a second argument to trigger. 添加第二个参数来触发。 The call becomes .trigger("click", [ true ]) 呼叫变为.trigger("click", [ true ])
  • name arguments in the event handler : function(event, simulated) 事件处理程序中的名称参数: function(event, simulated)
  • use the simulated argument which is set to true from the trigger : simulated || $(this).find('a[id$="lnkHidden"]').trigger("click", [ true ]); 使用从触发器设置为true的模拟参数: simulated || $(this).find('a[id$="lnkHidden"]').trigger("click", [ true ]); simulated || $(this).find('a[id$="lnkHidden"]').trigger("click", [ true ]);

However that event triggering and that kind of selectors are not recommended. 但是,不建议使用事件触发和这种选择器。

Instead of triggering synthetic click events, you could just change the current URL directly: 除了触发综合点击事件,您还可以直接更改当前URL:

$(document).ready(function () {
    $('table[id$="dataTable"]').find("tbody").on("click", "tr", function () {
        var links = $(this).find('a[id$="lnkHidden"]');
        if(links.length && links[0].href) {
            window.location.href = links[0].href;
        }
    });
});

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

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