简体   繁体   English

关于Ajax输出的onclick事件

[英]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 确保

  • you have jQuery script file is inserted in your html page, 你有jQuery脚本文件插入你的html页面,
  • you have assigned id id to one and only one item on your page, 您已将id id分配给页面上的一个且只有一个项目,
  • you give us a link to see your page if nothing else helps. 如果没有别的帮助,你给我们一个链接来查看你的页面。 :) :)

live is now deprecated. live现已弃用。 Use on instead. 使用on代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM