(function($) {
var SelectChoice = {
set: function() {
SelectChoice.level = 1;
if (settings.sid != 0) {
SelectChoice.name = _this.nextAll('select').last().attr('name');
SelectChoice.status = true;
_this.change(function(event) {
SelectChoice.level = $(this).index() + 1;
SelectChoice.getdata($(this).val());
if ($(this).val() == 0) {
$(this).nextAll('select').remove();
} else {
SelectChoice.getdata($(this).val());
}
});
_this.nextAll('select').change(function(event) {
SelectChoice.level = $(this).index() + 1;
SelectChoice.getdata($(this).val());
});
} else {
SelectChoice.name = _this.attr('name');
SelectChoice.status = false;
SelectChoice.getdata(settings.pid);
}
},
view: function() {
if (settings.data == null || settings.data == '') {
} else {
if (SelectChoice.level == 1 && SelectChoice.status == false) {
_this.change(function(event) {
SelectChoice.level = $(this).index() + 1;
if ($(this).val() == 0) {
$(this).nextAll('select').remove();
} else {
SelectChoice.getdata($(this).val());
}
});
_this.append('<option value="0">请选择</option>');
$.each(settings.data, function(index, el) {
_this.append('<option value="' + el.id + '">' + el.name + '</option>');
});
SelectChoice.status = true;
} else {
var select = $('<select class="select"><option value="0">请选择</option></select>');
select.attr('name', SelectChoice.name);
select.change(function(event) {
SelectChoice.level = $(this).index() + 1;
SelectChoice.getdata($(this).val());
});
$.each(settings.data, function(index, el) {
select.append('<option value="' + el.id + '">' + el.name + '</option>');
});
var this_dom = null;
if (SelectChoice.level == 1) {
_this.removeAttr('name').nextAll('select').remove();
this_dom = _this;
} else {
this_dom = _this.nextAll('select:eq(' + (SelectChoice.level - 2) + ')');
this_dom.nextAll('select').remove();
_this.removeAttr('name').nextAll('select').removeAttr('name');
}
this_dom.after(select);
}
}
},
//获取数据
getdata: function(pid) {
if (settings.data_url == null || settings.data_url == '') {
} else {
if (SelectChoice.level < settings.level) {
$.ajax({
url: settings.data_url,
type: 'POST',
dataType: 'json',
data: {
pid: pid,
},
async: false,
})
.done(function(data) {
if (data.status) {
settings.data = data.data;
} else {
settings.data = null;
}
})
.fail(function() {
console.log("error");
})
.always(function() {
console.log("complete");
});
SelectChoice.view();
}
}
},
}
var methods = {
init: function(options) {
settings = $.extend({}, defaults, options);
return this.each(function() {
SelectChoice.set();
});
},
};
var defaults = {
sid: 0,
pid: 0,
level: 4,
data_url: null,
data: null,
};
var settings = {}; //将一个空对象做为第一个参数
var _this = null;
$.fn.selectchoice = function(method) {
_this = this;
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.selectchoice');
}
};
})(jQuery);
/*********************************************************************************/
<script type="text/javascript">
jQuery(function($) {
var a = $('#abc').selectchoice({
'data_url':'<{:U("Area/getArea")}>',
});
var b= $('#abcd').selectchoice({
'data_url':'<{:U("Area/getArea")}>',
});
});
</script>
<select id="abc" name="abc"></select>
<select id="abcd" name="abc"></select>
var SelectChoice = {
set: function() {
SelectChoice.level = 1;
if (settings.sid != 0) {
SelectChoice.name = _this.nextAll('select').last().attr('name');
SelectChoice.status = true;
_this.change(function(event) {
SelectChoice.level = $(this).index() + 1;
SelectChoice.getdata($(this).val());
if ($(this).val() == 0) {
$(this).nextAll('select').remove();
} else {
SelectChoice.getdata($(this).val());
}
});
_this.nextAll('select').change(function(event) {
SelectChoice.level = $(this).index() + 1;
SelectChoice.getdata($(this).val());
});
} else {
SelectChoice.name = _this.attr('name');
SelectChoice.status = false;
SelectChoice.getdata(settings.pid);
}
},
view: function() {
if (settings.data == null || settings.data == '') {
} else {
if (SelectChoice.level == 1 && SelectChoice.status == false) {
_this.change(function(event) {
SelectChoice.level = $(this).index() + 1;
if ($(this).val() == 0) {
$(this).nextAll('select').remove();
} else {
SelectChoice.getdata($(this).val());
}
});
_this.append('<option value="0">请选择</option>');
$.each(settings.data, function(index, el) {
_this.append('<option value="' + el.id + '">' + el.name + '</option>');
});
SelectChoice.status = true;
} else {
var select = $('<select class="select"><option value="0">请选择</option></select>');
select.attr('name', SelectChoice.name);
select.change(function(event) {
SelectChoice.level = $(this).index() + 1;
SelectChoice.getdata($(this).val());
});
$.each(settings.data, function(index, el) {
select.append('<option value="' + el.id + '">' + el.name + '</option>');
});
var this_dom = null;
if (SelectChoice.level == 1) {
_this.removeAttr('name').nextAll('select').remove();
this_dom = _this;
} else {
this_dom = _this.nextAll('select:eq(' + (SelectChoice.level - 2) + ')');
this_dom.nextAll('select').remove();
_this.removeAttr('name').nextAll('select').removeAttr('name');
}
this_dom.after(select);
}
}
},
//获取数据
getdata: function(pid) {
if (settings.data_url == null || settings.data_url == '') {
} else {
if (SelectChoice.level < settings.level) {
$.ajax({
url: settings.data_url,
type: 'POST',
dataType: 'json',
data: {
pid: pid,
},
async: false,
})
.done(function(data) {
if (data.status) {
settings.data = data.data;
} else {
settings.data = null;
}
})
.fail(function() {
console.log("error");
})
.always(function() {
console.log("complete");
});
SelectChoice.view();
}
}
},
}
var methods = {
init: function(options) {
settings = $.extend({}, defaults, options);
return this.each(function() {
SelectChoice.set();
});
},
};
var defaults = {
sid: 0,
pid: 0,
level: 4,
data_url: null,
data: null,
};
var settings = {}; //将一个空对象做为第一个参数
var _this = null;
$.fn.selectchoice = function(method) {
_this = this;
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.selectchoice');
}
};
})(jQuery);
/*********************************************************************************/
<script type="text/javascript">
jQuery(function($) {
var a = $('#abc').selectchoice({
'data_url':'<{:U("Area/getArea")}>',
});
var b= $('#abcd').selectchoice({
'data_url':'<{:U("Area/getArea")}>',
});
});
</script>
<select id="abc" name="abc"></select>
<select id="abcd" name="abc"></select>

