首先,在表中的最後一個項目之後刪除逗號修復您的searchwithin
表中的語法錯誤。這會在IE6/IE7中導致錯誤。
然後,您可以使用此代碼來翻檢searchwithin
陣列找到id
爲適當type
,然後設置一個點擊事件處理程序爲id
。
var searchwithin = [
{id:"clearwithin", type:"button"},
{id:"search_radius", type:"select"},
{id:"for_lease" ,type:"checkbox"},
{id:"for_sale", type:"checkbox"}
];
function findIdByType(target) {
for (var i = 0; i < searchwithin.length; i++) {
if (searchwithin[i].type === target) {
return(searchwithin[i].id);
}
}
}
var id = findIdByType("button");
if (id) {
$("#" + id).click(function() {
// do whatever you want to do on the click function here
}
});
我注意到你的表有兩個條目type:checkbox
。上述代碼建議將僅返回並在第一個條目上運行。如果您想爲這兩個ID設置點擊處理程序,那麼代碼或表格將不得不進行修改。如果這是所有表被用於,它可以改變爲一個選擇器(它可以包含多個id),如下所示:
var searchwithin = [
{id:"#clearwithin", type:"button"},
{id:"#search_radius", type:"select"},
{id:"#for_lease, #for_sale", type:"checkbox"}
];
function findSelectorByType(target) {
for (var i = 0; i < searchwithin.length; i++) {
if (searchwithin[i].type === target) {
return(searchwithin[i].id);
}
}
}
var selector = findSelectorByType("button");
if (selector) {
$(selector).click(function() {
// do whatever you want to do on the click function here
}
});