0
我對jQuery相當陌生,想知道是否有人可以提供關於如何將我的代碼合併到下面的建議。我的表單有一組動態下拉框,其中第二個下拉框會根據第一個下拉框中的選擇顯示一組值。如何結合jQuery/AJAX功能?
我需要回憶窗體克隆上的AJAX,以便下拉框的動態功能正常工作。
任何想法?
$(document).ready(function(){
var sheepItForm = $('#clone').sheepIt({
separator: '',
allowRemoveLast: true,
allowRemoveCurrent: true,
allowAdd: true,
maxFormsCount: 3,
minFormsCount: 1,
iniFormsCount: 1
});
$(".item").change(function() {
var group_id = $(this).val();
var self = $(this); // Added line
var $children = $(this).parent().next().children('select.options')
$.ajax({
type: "POST",
url: "../../db/groups.php?id=" + group_id,
dataType: "json",
success: function(data){
$children.empty()
$children.append('<option value="">Select</option>');
$.each(data, function(i, val){
$children.append('<option value="' + val.group_id + '">' + val.name + '</option>');
});
$children.focus();
},
beforeSend: function(){
$children.empty();
$children.append('<option value="">Loading...</option>');
},
error: function(){
$children.attr('disabled', true);
$children.empty();
$children.append('<option value="">No Options</option>');
}
})
});
$('#group_add').live('click', function() {
$(".item").change(function() {
var group_id = $(this).val();
var self = $(this); // Added line
var $children = $(this).parent().next().children('select.options')
$.ajax({
type: "POST",
url: "../../db/groups.php?id=" + group_id,
dataType: "json",
success: function(data){
$children.empty()
$children.append('<option value="">Select</option>');
$.each(data, function(i, val){
$children.append('<option value="' + val.group_id + '">' + val.name + '</option>');
});
$children.focus();
},
beforeSend: function(){
$children.empty();
$children.append('<option value="">Loading...</option>');
},
error: function(){
$children.attr('disabled', true);
$children.empty();
$children.append('<option value="">No Options</option>');
}
})
});
}
})
嗨Roselan,我給這個試試吧!調查GET功能是什麼意思? – Michael 2012-01-07 21:55:08
$ .get和$ .post只是簡單的「助手」函數,它調用$ .ajax。如果你喜歡,你只需要更少的代碼來編寫代碼。 – roselan 2012-01-07 22:13:53