我發現自己做了這個反覆。jQuery通過ID篩選,然後捕獲匹配
$jq("button").filter(function(){
return this.id.match(/^user_(\d+)_edit$/);
}).click(function(){
var matches = this.id.match(/^user_(\d+)_edit$/);
var user_id = matches[1];
alert('click on user edit button with ID ' + user_id);
});
所以我想一個點擊事件適用於一些按鈕和Click事件處理程序,我需要的用戶ID。有沒有辦法避免第二場比賽?
$jq("button").filter(function(){
return this.id.match(/^user_(\d+)_edit$/);
}).click(function(){
var user_id = some_magic_variable;
alert('click on user edit button with ID ' + user_id);
});
謝謝。
+1比正則表達式匹配漂亮,並提供有用的建議。 – karim79 2009-06-29 18:18:48
我同意使用'edit_user'類,但我需要每個按鈕的user_id鏈接。我可以這樣做但我需要做匹配來獲取用戶ID。 – 2009-06-29 18:20:36