onclick event on Ajax output

旧时模样 提交于 2019-12-10 09:07:34

问题


Hi i am using Ajax to get a SELECT tag. 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.

I need an onclick event on that SELECT tag, i tried using JQuery

$('#id').click(function() {
  alert('test');
});

Its not working. Can anybody help, please


回答1:


Because the select tag is dynamically added to the HTML after the event was set, the event is not set on the select tag.

A simple solution is to use live() here:

$('#id').live('click', function() {
  alert('test');
});



回答2:


as you are dynically generating html use live instead of click

  $('#id').live('click', function() {
  // Live handler called.
          });



回答3:


Make sure

  • you have jQuery script file is inserted in your html page,
  • you have assigned id id to one and only one item on your page,
  • you give us a link to see your page if nothing else helps. :)



回答4:


live is now deprecated. Use on instead.



来源:https://stackoverflow.com/questions/4136008/onclick-event-on-ajax-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!