我使用jquery Ajax动态生成dropdownList,生成的dropdown的ID为specificationAttribute
。我想为生成的新标签创建添加事件(specificationAttribute
),为此我在以下方法script
中创建了Lowere window.load
:
$(document).on('change', '#specificationattribute', function () {
alert("Clicked Me !");
});
但它不起作用。我尝试任何方式都更像click
,live
但是我无法取得任何结果。
小提琴的代码:
$(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 ");
});
}
您的小提琴有语法错误。由于下拉列表会生成一个选择,因此我们使用一个。
对于我的回答,我使用了此HTML,稍后再介绍:您的代码中不匹配的内容
<select id="specificationAttribute" name="specificationAttribute">
</select>
代码已更新:(请参阅内联注释,其中一些是建议,一些错误)
$(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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句