回复 4楼 冰镇柠檬汁儿
看起来比较low的写法!有没有方法跟精简点,可读性好一点,有时我自己都看迷糊!这个函数目前题目中出现了A或其他字母的时候就会出错,选项会出问题
如果倒着分割,万一出现选项 A、B和F B、F和B C、B和C D、C和B,郁闷了
function answerOptions(id, type, text, num) {
var page = "";
var answerTypeName = "";
var newText = text;
newText = newText.replace(/A,/g, 'A、');
newText = newText.replace(/B,/g, 'B、');
newText = newText.replace(/C,/g, 'C、');
newText = newText.replace(/D,/g, 'D、');
newText = newText.replace(/A./g, 'A、');
newText = newText.replace(/B./g, 'B、');
newText = newText.replace(/C./g, 'C、');
newText = newText.replace(/D./g, 'D、');
newText = newText.replace(/\ +/g, '');
switch (type) {
case "0": answerTypeName = "单选题";
break;
case "1": answerTypeName = "判断题";
break;
case "2": answerTypeName = "多选题";
break;
}
if (type == "2") {
page += "<fieldset data-role=\'controlgroup\' name=\'answerList\' id=\'" + id + "||2\'>";
var strText = newText.split("A、");
for (i = 0; i < strText.length; i++) {
if (i == 1) {
var answerResults = "A、" + strText[i];
var myarray = answerResults.split(/[ABCD]/);
for (i = 0; i < myarray.length; i++) {
if (myarray[i] != "" && myarray[i] != "undefined") {
if (i == 1) {
page += "<label for=\'" + id + "||A\'>A" + myarray[i] + "</label><input type=\'checkbox\' name=\'" + id + "\' id=\'" + id + "||A\' value=\'1\'>";
} else if (i == 2) {
page += "<label for=\'" + id + "||B\'>B" + myarray[i] + "</label><input type=\'checkbox\' name=\'" + id + "\' id=\'" + id + "||B\' value=\'2\'>";
} else if (i == 3) {
page += "<label for=\'" + id + "||C\'>C" + myarray[i] + "</label><input type=\'checkbox\' name=\'" + id + "\' id=\'" + id + "||C\' value=\'3\'>";
} else if (i == 4) {
page += "<label for=\'" + id + "||D\'>D" + myarray[i] + "</label><input type=\'checkbox\' name=\'" + id + "\' id=\'" + id + "||D\' value=\'4\'>";
} else if (i == 5) {
page += "<label for=\'" + id + "||E\'>E" + myarray[i] + "</label><input type=\'checkbox\' name=\'" + id + "\' id=\'" + id + "||E\' value=\'5\'>";
}
}
}
} else {
page += "<legend>试题" + num + ":" + strText[i] + "(" + answerTypeName + ")</legend>";
}
}
page += "</fieldset>";
page += "<hr size=\'6px\' color=\'#0098FF\'/>";
} else {
page += "<fieldset data-role=\'controlgroup\' name=\'answerList\' id=\'" + id + "||1\'>";
var strText = newText.split("A、");
for (i = 0; i < strText.length; i++) {
if (i == 1) {
var answerResults = "A、" + strText[i];
var myarray = answerResults.split(/[ABCD]/);
for (i = 0; i < myarray.length; i++) {
if (myarray[i] != "" && myarray[i] != "undefined") {
if (i == 1) {
page += "<label for=\'" + id + "||A\'>A" + myarray[i] + "</label><input type=\'radio\' name=\'" + id + "\' id=\'" + id + "||A\' value=\'1\'>";
} else if (i == 2) {
page += "<label for=\'" + id + "||B\'>B" + myarray[i] + "</label><input type=\'radio\' name=\'" + id + "\' id=\'" + id + "||B\' value=\'2\'>";
} else if (i == 3) {
page += "<label for=\'" + id + "||C\'>C" + myarray[i] + "</label><input type=\'radio\' name=\'" + id + "\' id=\'" + id + "||C\' value=\'3\'>";
} else if (i == 4) {
page += "<label for=\'" + id + "||D\'>D" + myarray[i] + "</label><input type=\'radio\' name=\'" + id + "\' id=\'" + id + "||D\' value=\'4\'>";
} else if (i == 5) {
page += "<label for=\'" + id + "||E\'>E" + myarray[i] + "</label><input type=\'radio\' name=\'" + id + "\' id=\'" + id + "||E\' value=\'5\'>";
}
}
}
} else {
page += "<legend>试题" + num + ":" + strText[i] + "(" + answerTypeName + ")</legend>";
}
}
page += "</fieldset>";
page += "<hr size=\'6px\' color=\'#0098FF\'/>";
}
return page;
}
[
本帖最后由 butterfeild 于 2015-5-6 19:54 编辑 ]