我有困难与jQuery遍历。我有一个带有一组动态下拉框的表单,并且正在尝试根据元素类而不是id调用特定的动作,因为此表单可以在我的应用程序中克隆。如何在AJAX函数中遍历jQuery?
任何想法为什么我的遍历不起作用?我的$(this)
电话会以某种方式失效吗?
HTML:
<div>
<label>Item:</label>
<select class="item" name="item" id="item">
<option value="">Select</option>
...
</select>
</div>
<div>
<label class="label>Options:</label>
<select class="options" name="options" id="options">
...
/select>
</div>
的Javascript:
$(".item").change(function() {
var group_id = $(this).val();
$.ajax({
type: "POST",
url: "../../db/items.php?id=" + group_id,
dataType: "json",
success: function(data){
$(this).parent().next().children('select.options').empty();
$(this).parent().next().children('select.options').append('<option value="">Select</option>');
$.each(data, function(i, val){
$(this).parent().next().children('select.options').append('<option value="' + val.group_id + '">' + val.name + '</option>');
});
$(this).parent().next().children('select.options').focus();
},
beforeSend: function(){
$(this).parent().next().children('select.options').empty();
$(this).parent().next().children('select.options').append('<option value="">Loading...</option>');
},
error: function(){
$(this).parent().next().children('select.options').attr('disabled', true);
$(this).parent().next().children('select.options').empty();
$(this).parent().next().children('select.options').append('<option value="">No Options</option>');
}
})
});
是的,在回调中,'this'指的是别的东西。如果您想要对DOM元素进行引用,则必须先存储它。 – 2012-01-06 22:40:52
谢谢,但存储它如何? – Michael 2012-01-06 22:41:30
可能重复[$(this)在AJAX成功不起作用](http://stackoverflow.com/questions/6394812/this-inside-of-ajax-success-not-working) – 2012-01-06 22:41:43