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
}
});