使用关联数组中的数据作为事件处理程序的对象
下午
我有一些数据 -
var searchwithin = [
{id:"clearwithin", type:"button"},
{id:"search_radius", type:"select"},
{id:"for_lease" ,type:"checkbox"},
{id:"for_sale", type:"checkbox"},
];
它将根据单击的内容显示和隐藏信息(这只是数据的一小部分。
我已经弄清楚如何检查类型属性,我想知道的是以下内容:
如果类型是按钮,我如何访问该项目的 id 值,然后生成一个单击函数,以便使用 id 作为与该函数关联的按钮的名称
预先感谢
菲尔。
Afternoon All
I have some data -
var searchwithin = [
{id:"clearwithin", type:"button"},
{id:"search_radius", type:"select"},
{id:"for_lease" ,type:"checkbox"},
{id:"for_sale", type:"checkbox"},
];
It is going to be showing and hiding informtion dependent on what is clicked (this is only a small fragment of the data.
I have already worked out how to check the type property, what I want to know is the following:
If the type is button, how do I access the id value for that items and then generate a click function so that that uses the id as the name of the button to associate the function with.
Thanks in advance
Phil
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,通过删除表中最后一项后面的逗号来修复
searchwithin
表中的语法错误。这会导致 IE6/IE7 出现错误。然后,您可以使用此代码查看
searchwithin
数组以查找相应type
的id
,然后设置一个点击事件处理程序那个id
。我注意到您的表格有两个
type:checkbox
条目。上面的代码建议将返回并仅对第一个条目进行操作。如果您想为这两个 ID 设置点击处理程序,则必须修改代码或表。如果这就是表的全部用途,则可以将其更改为选择器(可以包含多个 id),如下所示:First, fix a syntax error in your
searchwithin
table by removing the comma after the last item in the table. That will cause an error in IE6/IE7.Then, you can use this code to look through your
searchwithin
array to find theid
for the appropriatetype
and then set a click event handler for thatid
.I notice that your table has two entries for
type:checkbox
. The above code suggestion will return and operate on the first entry only. If you want to set up click handlers for both those IDs, then either the code or table would have to be modified. If this is all the table is used for, it could be changed to be a selector (which can contain more than one id) like this:连接“#”和 id 属性并使用生成的字符串作为选择器。
考虑使用选择器来查找元素而不是 ID 映射。
Concatenate '#' and the id property and use the resulting string as a selector.
Consider using selectors to find elements rather than ID maps.