前台代码:
从这里读取分类的属性设置:
$.getJSON(G_BASE_URL + '/publish/ajax/category_attr/' + id, function (d) {
category_attr = d;
$.loading('hide');
var c = $('.aw-publish-type').html('');
for (var i = 0; i < d.length; i++) {
var a = $('<A/>').attr('trade', d[i].tid);
var n = '';
switch (d[i].tid) {
case 1: n = '出售'; break;
case 2: n = '求购'; break;
case 3: n = '出租'; break;
case 4: n = '求租'; break;
case 5: n = '合作'; break;
case 6: n = '任务'; break;
case 7: n = '招聘'; break;
default:
}
if (i == 0) a.addClass('first');
if (i == d.length - 1) a.addClass('last');
a.html(n).appendTo(c);
}
var g = $('.aw-publish-title-dropdown.aw-publish-title-dropdown-question');
if (d.length > 0) {
g.removeClass('no-type');
} else {
g.addClass('no-type');
}
$('.aw-publish-type a').click(function () {
select_attr_form($(this).index());
});
var trade_id = $('input[name=trade_id]').val();
var index = $('.aw-publish-type a[trade=' + trade_id + ']').index();
index = index == -1 ? 0 : index;
select_attr_form(index);
});
//输出表单
function select_attr_form(index) {
var ta = $('.aw-publish-type a').eq(index);
ta.addClass('active').siblings().removeClass('active');
$('input[name=trade_id]').val(ta.attr('trade'));
var c = $('.aw-publish-attr').fadeOut().html('');
var ct = category_attr[index];
var d = ct.form;
if (ct.user_info_count >= ct.limit && ct.limit > 0) {
var wn = '本交易类型限制每会员每天发布' + ct.limit + '条信息,您已经发布了' + ct.user_info_count + '条!';
$('.aw-warning').fadeIn().find('span').html(wn); freeze(true);
} else {
$('.aw-warning').fadeOut().find('span').html(''); freeze(false);
}
for (var i = 0; i < d.length; i++) {
var de = $('span#' + d[i].title), dv = '';
if (de.length > 0) dv = de.html();
var dl = $('<DL/>');
var h = '<dt><label class="required">' + d[i].title + ':</label></dt><dd>';
switch (d[i].type) {
case 'text':
h += '<input type="text" value="' + dv + '" style="width:' + d[i].width + 'px" name="attrs[' + d[i].title + ']" maxlength="' + d[i].len + '" placeholder="' + d[i].cnt + '" />';
break;
case 'radio':
var a = d[i].cnt.split('|');
h += '<div class="radios">';
for (var j = 0; j < a.length; j++) {
h += '<input type="radio" id="attrs_' + i + '_' + j + '" name="attrs[' + d[i].title + ']"';
if (dv == a[j]) {
h += ' checked ';
}
h += 'value="' + a[j] + '" /><label for="attrs_' + i + '_' + j + '">' + a[j] + '</label>';
}
h += '</div>';
break;
case 'select':
h += '<select name="attrs[' + d[i].title + ']">';
var a = d[i].cnt.split('|');
for (var j = 0; j < a.length; j++) {
h += '<option ';
if (dv == a[j]) {
h += ' selected ';
}
h += 'value="' + a[j] + '">' + a[j] + '</option>';
}
h += '</select>';
break;
default: h += ' ';
}
if (d[i].hint) {
h += '<p class="hint">' + d[i].hint + '</p>';
}
h += '</dd>';
dl.html(h).appendTo(c.stop().fadeIn());
}
}
要建一张表来存储问题的属性
后台在分类设置里加一个字段,保存属性信息,我比较懒,直接写json放进去,读得时间直接输出