2010-07-17 16 views
0

我必须修改以下函数,以便它只将click事件绑定到所有href的= /ShoppingCart.asp?ProductCode="whatever“(whatever = whatever在那里“),但不是,如果它具体是/ShoppingCart.asp?ProductCode="GFT”。它还必须检查或转换gft或Gft以大写以检查这些以及基本上它必须检查任何。 GFT的情况下的变化。如果它找到一个“GFT”不click事件绑定修改点击事件脚本更具体

function sacsoftaddtocart() { 
    if (location.pathname == "/SearchResults.asp" || location.pathname == "/Articles.asp" || location.pathname.indexOf("-s/") != -1 || location.pathname.indexOf("_s/") != -1) { 
     $("a[href^='/ShoppingCart.asp?ProductCode']").click(function() { 
      var href = $(this).attr('href'); 
      addToCart3(href); 
      return false; 
     }); 
    } 
} 

回答

1

可以使用.toUpperCase().filter(),这样做:。

function sacsoftaddtocart(){ 
    if (location.pathname == "/SearchResults.asp" || location.pathname == "/Articles.asp" || location.pathname.indexOf("-s/") != -1 || location.pathname.indexOf("_s/") != -1) { 
    $("a[href^='/ShoppingCart.asp?ProductCode']").filter(function() { 
     return this.href.length - this.href.toUpperCase().indexOf('PRODUCTCODE=GFT') != 15; 
    }).click(function() { 
     var href = $(this).attr('href'); 
     addToCart3(href); 
     return false; 
    }); 
    } 
} 

You cant test it in a demo herethis.href.length - matchPosition == 15正在检查ProductCode=GFT是否匹配“GFT”之后没有任何内容,因此产品代码(如“GFT5”)不匹配。

+0

曾任职完全和是的,我不想引用GFT,但我明白你做了什么并修复了它。再次感谢您的帮助Nick,而不是您第一次帮助我!你达人!!! – user357034 2010-07-17 13:43:05

+0

@ user357034 - 欢迎,我为未来的用户更新了不想引用的答案:) – 2010-07-17 13:43:45

+0

这是否意味着如果其他产品是GFT5或5gft,它会绑定? – user357034 2010-07-17 13:46:10

0

使用过滤器在这个岗位link text

function sacsoftaddtocart() { 
    if (location.pathname == "/SearchResults.asp" || location.pathname == "/Articles.asp" || location.pathname.indexOf("-s/") != -1 || location.pathname.indexOf("_s/") != -1) { 
     $("a:regex('href','*/ShoppingCart.asp\?ProductCode=(!?=GFT)*)").click(function() { 
      var href = $(this).attr('href'); 
      addToCart3(href); 
      return false; 
     }); 
    } 
} 

,或者如果你不想使用exrat插件:

function sacsoftaddtocart() { 
    if (location.pathname == "/SearchResults.asp" || location.pathname == "/Articles.asp" || location.pathname.indexOf("-s/") != -1 || location.pathname.indexOf("_s/") != -1) { 
     $("a['href^='/ShoppingCart.asp?ProductCode']") 
      .filter(function(){ return !/ProductCode=GTF/.test($(this).attr('href')) }; 
      .click(function() { 
       var href = $(this).attr('href'); 
       addToCart3(href); 
       return false; 
     }); 
    } 
} 

试试吧,看看会发生什么;)