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要素への参照が必要な場合は、先に格納する必要があります。 –
ありがとうございますが、それをどのように保存しますか? – Michael
AJAXの成功の中の[$(this)内部が正常に動作しない](http://stackoverflow.com/questions/6394812/this-inside-of-ajax-success-not-working) –