代码之家  ›  专栏  ›  技术社区  ›  David Yell

jquery~跟踪前调整href

  •  2
  • David Yell  · 技术社区  · 16 年前

    我有一个带有一系列复选框的链接,这是由于遗留代码的缘故,当用户单击一个复选框时,它的值将作为逗号分隔列表附加到href。

    问题是,现在我正在更新代码,我发现HREF的跟踪速度比正在调整的HREF快,因此列表被排除在外。

    我试过用 preventDefault() 这很好,但我不知道如何在更改了href以包含所选值后继续默认操作。

    我还应该提到,将其更改为常规形式是不可行的,因为在 提交后的灯箱 . (别问我为什么,在我的书中这是精神错乱)

    到目前为止,我必须

    $(function(){
        $('#cnt-area form table tbody').dragCheck();
    
        // I just split up the .each and .click to see if it mattered, which it doesnt
        $('.multi_assign_link').each(function(){
            $(this).click(function(e){
    
                e.preventDefault();
                var href = $(this).attr('href');
    
                $('#cnt-area form input:checkbox').each(function(){
                    if($(this).attr('checked')){
                        if(href.search(','+$(this).val()) == -1){
                            href += ','+$(this).val();
                        }
                    }else{
                        var s = ','+$(this).val();
                        s.toString();
                        href = href.replace(s, '');
                    }
                });
                $(this).attr('href',href);
                // Continue to follow the link
                // Faking a user click here with $(this).click(); obviously throws a loop!
            });
        });
    });
    
    1 回复  |  直到 16 年前
        1
  •  5
  •   tvanfosson    16 年前

    只需将位置设置为调整后的href值,并从处理程序返回false,以避免使用原始链接。

    $(function(){ 
        $('#cnt-area form table tbody').dragCheck(); 
    
        // I just split up the .each and .click to see if it mattered, which it doesnt 
        $('.multi_assign_link').each(function(){ 
            $(this).click(function(e){ 
    
                var href = $(this).attr('href'); 
    
                $('#cnt-area form input:checkbox').each(function(){ 
                    if($(this).attr('checked')){ 
                        if(href.search(','+$(this).val()) == -1){ 
                            href += ','+$(this).val(); 
                        } 
                    }else{ 
                        var s = ','+$(this).val(); 
                        s.toString(); 
                        href = href.replace(s, ''); 
                    } 
                }); 
                location.href = href;
                return false;
            }); 
        }); 
    });