我通过JSON返回数据并尝试循环遍历JSON数据集以动态填充表单。我需要通过该字段的id属性引用特定字段TYPE(选择,文本,复选框等)。我匹配的id属性是在JSON列中传递的。即,JSON中的每个列值对应于表单字段的ID属性。使用该列值,我需要找出它对应的表单字段TYPE是什么,以便我知道如何处理该字段的填充,即如果它是一个选择框,插入不同于文本字段等。这是到目前为止我有什么。它正好填充文本字段(因为它不检查字段类型),但我需要帮助访问和填充复选框,选择,ckeditor等。
{"COLUMNS":["RECORD_ID","USER_ID","PRODUCT_NAME","MODEL_NUMBER","KEYWORDS","CATEGORIES","PRODUCT_GROUP_ID","DESCRIPTION","ORIGIN","SHIPPING_PORTS","MOQ","MOQ_UNIT","FOB_PRICE_CURRENCY","FOB_PRICE","LEAD_TIME","CAPACITY","CAPACITY_UNIT","SAMPLE_CURRENCY","SAMPLE_PRICE","SAMPLE_LEAD_TIME","PACKING","STANDARDS","SHIP_SINGLE","SINGLE_LEAD_TIME","PAYMENT_TERMS","SHOW_LIVE","MAKE_SELL_OFFER","RECEIVE_ALERTS","ALERT_KEYWORDS","BRAND_NAME","IMAGE1","IMAGE2","IMAGE3","IMAGE4","IMAGE5","IMAGE6","IMAGE7","IMAGE8","DOCUMENT1","DOCUMENT2","EXPIRY_DATE","CREATED_DATE","UPDATED_DATE","EXPIRED_FLAG","DISABLED"],"DATA":[[2,18,"Product Title "," * Model Number","Keywords","",17,"<p>\r\n\tsdadsafsasdasdasdasd<\/p>",0.0," \tShipping Ports","Minimum Order (MOQ)","Piece\/s","EUR","Please Enquire","MOQ Lead Time","Monthly Production Capacity","Piece\/s","EUR","Sample Price","Sample Lead Time","According to your requirements","Standards Compliance (e.g. cUPC, UPC, UL)",true,"Lead Time for single unit","L\/C,T\/T,D\/A",true,true,true,"bathroom vanities, bathroom mirrors, tiles, faucets",null,"17431~Sacred-Heart-of-Jesus-Posters.jpg","","","","","","","","","",null,"October, 09 2010 00:00:00","October, 13 2010 00:00:00",false,false]]}
var product_id = $("#product_id").val();
$.getJSON("/cfcs/main.cfc?method=getProducts&returnformat=json",{"product_id":product_id},function(res,code) {
//loop over each row
for (i = 0; i < res.DATA.length; i++) {
//loop over each column
for (j = 0; j < res.DATA[i].length; j++) {
var field = res.COLUMNS[j].toString().toLowerCase();
var value = res.DATA[i][j].toString();
// Here I want to check field type so I know how to insert data. But, I'm not sure how to go about it. Following is pretty much just pseudo code...
if(field){
if (field.type == 'checkbox' && value == 1) {
field.checked = true;
}else if(field.type == 'select'){
var options = $("#" + field).attr('options');
if(options.length == 0){
options[options.length] = new Option(value, field, true, true);
}else{
$("#" + field).val(value);
}
}else if(field.type == 'textarea'){
CKEDITOR.instances[field].setData(value)
}else {
$("#" + field).val(value);
}
}
}
}
});
答案 0 :(得分:5)
如果field
是一个对象,那么你应该这样做:
$('#' + field.id)...
但是当你从JSON获得值时,它是一个字符串。所以你应该这样做:
$('#' + field).attr('type')
而不是
field.type