javascript - change-händelse fungerar inte på dynamiskt genererade element - Jquery
Jag genererar en dropdown-lista dynamiskt med jQuery Ajax, den genererade dropdownens id är specificationAttribute. Jag vill skapa ett lägg till-evenemang för den nya taggen som genererades (specificationAttribute), för att göra detta skapade jag nedanstående script i window.load:
$(document).on('change', '#specificationattribute', function () {
alert("Clicked Me !");
});
men det fungerar inte. Jag försöker på vilket sätt som helst, till exempel click, live, men jag får inget resultat.
jsfiddle
Kod från fiddlen:
$(window).load(function () {
$("#specificationCategory").change(function () {
var selected = $(this).find(":selected");
if (selected.val().trim().length == 0) {
ShowMessage('please selecet ...', 'information');
}
else {
var categoryId = selected.val();
var url = $('#url').data('loadspecificationattributes');
$.ajax({
url: url,
data: { categoryId: categoryId, controlId: 'specificationattribute' },
type: 'POST',
success: function (data) {
$('#specificationattributes').html(data);
},
error: function (response) {
alert(response.error);
}
});
}
});
$(document).on('change', '#specificationAttribute', function () {
alert("changed ");
});
}
Svar #1
Din fiddle har syntaxfel. Eftersom en dropdownlista genererar en select, låt oss använda en.
För mitt svar använde jag DENNA HTML, mer om detta senare: saker stämde inte i din kod
<select id="specificationAttribute" name="specificationAttribute">
</select>
Kod uppdaterad: (se inline-kommentarer, vissa är förslag, vissa fel)
$(window).on('load', function() {
$("#specificationCategory").on('change',function() {
var selected = $(this).find(":selected");
// if there is a selection, this should have a length so use that
// old: if (selected.val().trim().length == 0) {
if (!selected.length) { // new
// NO clue what this is and not on the fiddle so commented it out
// ShowMessage('please selecet ...', 'information');
alert("select something a category");// lots of ways to do this
} else {
var categoryId = selected.val();
var url = $('#url').data('loadspecificationattributes');
$.ajax({
url: url,
data: {
categoryId: categoryId,
controlId: 'specificationattribute'
},
type: 'POST',
success: function(data) {
// THIS line id does not match my choice of specificationAttribute so I changed it
$('#specificationAttribute').html(data);
},
error: function(response) {
alert(response.error);
}
});
}
});
// THIS should work with the markup I put as an example
$(document).on('change', '#specificationAttribute', function() {
alert("changed ");
});
});// THIS line was missing parts
Svar #2
@Uthman, det kan vara så att du har gett olika id för select och använder fel id i onchange-händelsen som jag observerade i jsfiddle-länken https://jsfiddle.net/a65m11b3/4/`
success: function (data) {
$('#specificationattributes').html(data);
},and $(document).on('change', '#specificationAttribute', function () {
alert("changed ");
}); $(document).on('change', '#specificationAttribute', function () {
alert("changed ");
});.
