首先,在表中的最后一个项目之后删除逗号修复您的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
}
});