2012-05-21 46 views
0

我是新來的jquery。我的要求是在jquery函數中傳遞rowid(表中每個記錄的唯一ID)。我只能在運行時獲得rowid。所以我怎樣才能將點擊事件綁定到id爲這個rowid的標籤。如何在jquery函數中傳遞運行時參數?

<a href="javascript:void(0)" id="`string(rowid(Gatepass))`">Upd</a> 
<a href="javascript:void(0)" id="`string(rowid(Gatepass))`">Del</a> 

$(what to pass here).bind('click',function(ev) { 
    _operation(para1,para2); // function which is going to perfom action 
    ev.preventDefault(); 
    return false; 
}); 
+0

不要指望,如果你有很大幫助什麼都做downvote所有的答案,甚至沒有評論 – Johan

+0

@Johan:我新來這個網站,所以不知道這是什麼「downvote」? – newprogress

回答

0

得到id和acording到ID你想

$('a').on('click',function(){ 

    var id = $(this).attr('id'); 

    if(id == //what you want) 
    { 
     //function 1 
    } 
    else 
    { 
     //function 2 
    }  

    return false; 

    }); 
0

如果ID從服務器動態地來了,把裏面的功能相同的id +哈希ID selctor

$(what to pass here) => $("'#" + string(rowid(Gatepass)) + "'") 
0

如果在ID之間的任何相似之處,你可以使用的一個屬性選擇器如:

ID包含

$('[id*="something"]') 

ID始於

$('[id^="something"]') 

http://api.jquery.com/category/selectors/

一個更好的方法可能是將所有的動態命名錨放入一個容器,然後在該選擇:

<div id="container"> 
    <a ...></a> 
    <a ...></a> 
</div> 

,那麼你會選擇所有子錨點:

$('#container > a').click(...); 
+0

請解釋你的downvotes。 – jbabey

0

很難從這麼少的HTML代碼中找到好的選擇器。如果可能,請在您的標記上使用一個類:

<a class="roww" href="javascript:void(0)" id="`string(rowid(Gatepass))`">Upd</a> 
<a class="roww" href="javascript:void(0)" id="`string(rowid(Gatepass))`">Del</a> 

然後您可以使用$('。roww')來查詢您的節點。

這裏是你可以做什麼來從事件處理程序的ID:

function(ev) { 
    //wrap the element with jQ: 
    var jel = $(this); 
    //Then access attributes with .attr() getter: 
    var id = jel.attr('id'); 

    ... //do whatever you want now. 
    ... // there's a quicker alternative to get the id without jQuery, simply: 
    ... // var id = this.id 
} 
相關問題