1. 程式人生 > >h5 生成json 字串

h5 生成json 字串

h5  生成json  字串 

var shenfen=$('.shenfen').val()
if(SurveysType==0){

if(edit == 1||edit==3) {
var henmo;
henmo = '';
$('.hengmoya input').each(function() {
henmo += $(this).val() + ",";
});
var yaliearr;
yaliearr = '';
$('.yalie input').each(function() {
yaliearr += $(this).val() + ",";
});


var titlelen = $('.tim').length;
//alert(titlelen)
var valact1 = $('.act_name1').val()
                
//提交問卷
var reg = /^0?1[3|4|7|5|8][0-9]\d{8}$/;
//if(!reg.test(valact1)) {
//mui.toast('請輸入正確手機號~')
//} else {
function person(CustomerId, SurveyQuestionMainId, SurveyQuestionId, AskId, AskVal, AskAndAnswerResult, AskAndElse,Id) {
this.CustomerId = CustomerId;
this.Id = Id;
this.SurveyQuestionMainId = SurveyQuestionMainId;
this.SurveyQuestionId = SurveyQuestionId;
this.AskId = AskId;
this.AskVal = AskVal;
this.AskAndAnswerResult = AskAndAnswerResult;
this.AskAndElse = AskAndElse;
}
var arr = [];
for(var i = 0; i < titlelen; i++) {
var questiontype = $('.tim').eq(i).attr('datatype');
var CustomerId = doctorid;
var SurveyQuestionMainId = idquestion;
var SurveyQuestionId = $('.tim').eq(i).attr('id');
var AskId;
var Id=$('.tim').eq(i).attr('dataid');
var AskVal;
var AskAndElse;
var AskAndAnswerResult;
var jsonhen = '';
var jsonlie = '';
//var isqu= $('.tim .IsRequired').eq(i).html();
//alert(isqu)
vaisqu= $('.tim .IsRequired').eq(i).html();
var henmolen = $('.hengmoya li').length;
var yalielen = $('.yalie input').length;
for(var g = 0; g < henmolen; g++) {
var kuohen = "\"" + $('.hengmoya li input').eq(g).attr('name') + "\":" + "\"" + $('.hengmoya li input').eq(g).val() + "\""
if(g == henmolen - 1) {
jsonhen += kuohen;
} else {
jsonhen += kuohen + ",";
}


}
$('.yalie li p').eq(g).html()
for(var g = 0; g < yalielen; g++) {
var kuolie = "\"" + 'y' + $('.yalie li p').eq(g).html() + "\"" + ":" + "\"" + $('.yalie li input').eq(g).val() + "\""


if(g == yalielen - 1) {
jsonlie += kuolie;
} else {
jsonlie += kuolie + ",";
}


}
console.log(jsonhen)
//alert(questiontype)
if(questiontype == 1) {
var br = '{ "hemo": {'


+jsonhen +


'},"yalie":{'


+
jsonlie +


'}}'
AskAndAnswerResult = br
AskVal = null
AskId = null
} else if(questiontype == 2) {
AskAndAnswerResult = $('.tim').eq(i).find('textarea').val();
var isqu= $('.tim .IsRequired').eq(i).attr('dataName');
if(AskAndAnswerResult==''&&isqu=='必填'){
mui.toast('有必填項未填寫');
return false;
}
AskVal = null
AskId = null
} else if(questiontype == 6) {
AskAndAnswerResult = $('.tim').eq(i).find('input').val();
var isqu= $('.tim .IsRequired').eq(i).attr('dataName');
if(AskAndAnswerResult==''&&isqu=='必填'){
mui.toast('有必填項未填寫');
return false;
}
AskVal = null
AskId = null
} else if(questiontype == 7) {
AskAndAnswerResult = $('.tim').eq(i).find('input').val();
var isqu= $('.tim .IsRequired').eq(i).attr('dataName');
if(AskAndAnswerResult==''&&isqu=='必填'){
mui.toast('有必填項未填寫');
return false;
}
AskVal = null
AskId = null
}else if(questiontype == 8) {
AskAndAnswerResult = $('.tim').eq(i).find('input').val();
var isqu= $('.tim .IsRequired').eq(i).attr('dataName');
if(AskAndAnswerResult==''&&isqu=='必填'){
mui.toast('有必填項未填寫');
return false;
}
AskVal = null
AskId = null
}else if(questiontype == 4) {
AskId = $('input:radio[name="' + SurveyQuestionId + '"]:checked').attr('id');
//alert(AskId)

AskVal = $('input:radio[name="' + SurveyQuestionId + '"]:checked').val();
var isqu= $('.tim .IsRequired').eq(i).attr('dataName');
                            if($('input:radio[name="' + SurveyQuestionId + '"]:checked').length==0&&isqu=='必填'){
mui.toast('有必填項未填寫');
return false;
}
AskAndElse = $('input:radio[name="' + SurveyQuestionId + '"]:checked').siblings().val();


AskAndAnswerResult = ''
} else if(questiontype == 5) {
var lengthin = $('input:checkbox[name="' + SurveyQuestionId + '"]:checked').length;
//alert(lengthin)


//for(var hh = 0; hh < lengthin; hh++) {
var isqu= $('.tim .IsRequired').eq(i).attr('dataName');
AskId = $('.tim').eq(i).find('input[type="checkbox"]:checked').attr('id');
AskVal = $('.tim').eq(i).find('input[type="checkbox"]:checked').val();
AskAndElse = $('input:text[name="' + AskId + '"]').val();
if($('input:checkbox[name="' + SurveyQuestionId + '"]:checked').length==0&&isqu=='必填'){
mui.toast('有必填項未填寫');
return false;
}


AskAndAnswerResult = ''

}


var myobj = new person(CustomerId, SurveyQuestionMainId, SurveyQuestionId, AskId, AskVal, AskAndAnswerResult, AskAndElse,Id);
//      alert(arr[i].SurveyQuestionId)s
var aa = JSON.stringify(myobj)
//讀取資料時要將其轉換成JSON格式資料
var obj = JSON.parse(aa);
console.log(obj);


arr.push(obj);
var orderId = arr;
console.log(orderId)
//}


//      arr.push(obj);


}