[英]onclick event on Ajax output
Hi i am using Ajax to get a SELECT tag. 嗨,我正在使用Ajax获取SELECT标记。 I mean, if i click on a button it will generate a SELECT tag inside HTML, i have different outputs for different options of select.
我的意思是,如果我点击一个按钮,它将在HTML中生成一个SELECT标签,我有不同的输出选择不同的选项。
I need an onclick event on that SELECT tag, i tried using JQuery 我在SELECT标签上需要一个onclick事件,我尝试使用JQuery
$('#id').click(function() {
alert('test');
});
Its not working. 它不起作用。 Can anybody help, please
请有人帮忙吗
Because the select tag is dynamically added to the HTML after the event was set, the event is not set on the select tag. 因为在设置事件后动态地将select标记添加到HTML,所以不在select标记上设置事件。
A simple solution is to use live()
here: 一个简单的解决方案是在这里使用
live()
:
$('#id').live('click', function() {
alert('test');
});
as you are dynically generating html use live instead of click 因为你正在动态地生成html使用live而不是click
$('#id').live('click', function() {
// Live handler called.
});
Make sure 确保
id
id to one and only one item on your page, id
id分配给页面上的一个且只有一个项目, live
is now deprecated. live
现已弃用。 Use on
instead. 使用
on
代替。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.