问题
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