2011-11-11 72 views
2

我正在使用jquery。我有一个在xslt中动态生成的锚标签。每个锚都必须打开一个隐藏的div,并在onclick事件中传递不同的值。与锚点标签和隐藏Div的jQuery Selector问题

在隐藏的div中,我有2个不同的按钮,一个用于取消其他确认操作。这个想法是:当我点击确认按钮previus锚(之前点击显示隐藏的div)获得其他样式(我使用addClass,工作正常)。我通过点击函数的值$(this)创建一个引用,它的效果很好...

问题:当我点击不同的锚点(显示div)时,先前被点击获取新值。例如,如果我点击“a”按钮,显示div,点击取消(没有什么反应那很好),然后我点击“b”按钮并点击确认,“a”和“b”得到样式。

我只需要在选定的按钮上显示样式,但没有清楚的想法我该怎么做。

我的代码:

function showInfo(team, selectName, selectId, temps, thisid){ 
    //Create a function that will recieves some values    
var showInfoLink = thisid; //Asigh a new variable the value of thisid ($(this)) 
showInfoLink.removeClass("highlighted"); 

//Show the component 
$("#com_advanceBet").css('z-index' , '1'); 

//in the component is a select, it will show the name on the anchor of the selected item     
$("select").change(function() { 
    var str = ""; 
    $("select option:selected").each(function() { 
    str += $(this).text() + " "; 
    }); 
    showInfoLink.html(str); 
}) 
.change(); 

//once i click here the anchor will have this class 
    $('#com_continueBet').click(function(){ 
    showInfoLink.removeClass('nostyle'); 
    showInfoLink.addClass('highlighted'); 

//hide the component 
    $("#com_advanceBet").css('z-index' , '-999999'); 
    });    
} 

这是锚:

<a href="#" onclick="showInfo('{$team}', '{$selectName}', '{$selectId}', '{$temps}', $(this)); " class="nostyle" ><xsl:value-of select="$pt" disable-output-escaping="yes"/><span class="test" style="font-size:0.65em"><xsl:value-of select="$odds" disable-output-escaping="yes"/></span></a> 

在锚,我通过$(this)的功能,但对于任何原因,对所有的previus值点击锚。

回答

1

我创造的这个上的jsfiddle一个模拟版本:http://jsfiddle.net/dPZJc/6/

它大大简化和似乎是工作,因为它应该。

+0

Thx,工作,但不是我多么希望..我的意思是,我不能将ID添加到锚点,因为它们是没有ID生成dinamically,我有很多IDS点击时运行相同的功能,问题是我在哪里有很多指针('$(this)')超出范围(我猜),并引发一些问题... –

+0

它们是从JavaScript还是服务器端脚本语言生成的? ID应该是不相关的;正如你在脚本中看到的那样,它们只是用于测试以确保'$(this)'在正确的上下文中。如果你用'alert(thisid.attr('id');')取消注释该函数的第一行,那么你会发现它_does_引用了正确的对象(在这个例子中是一个DOM元素)。 –