我在 extjs+yii 工作。我的看法是——
Ext.define('Balaee.view.qb.qbqns.QbqnsView',
{
extend:'Ext.view.View',
id:'qbqnsViewId',
alias:'widget.QbqnsView',
store:'qb.QbqnsStore',
autoScroll: true,
config:
{
tpl:'<tpl for=".">'+
'<div id="main">'+
'</br>'+
// '<b>Question :-</b> {pollQuestion}</br>'+
'<h1 id="q">Question :-</h1> {question} </br>'+
'<tpl for="options">'+ // interrogate the kids property within the data
'<p>  <input type="radio" name="{parent.questionId}" value="{optionId}"> {option}</p>'+
//'<p>  <input type="radio" name="{questionId}"> {option}</p>'+
'</tpl></p>'+
'<p>---------------------------------------------------------</p>'+
'</div>'+
'</tpl>',
itemSelector:'div.main'
}
});// End of login class
此视图显示问题及其相关选项。现在,在选择答案并单击提交按钮后,我想发送 Questionid 及其选择的单选按钮选项。随着数据采用 json 格式,我正在捕获这些选定的单选按钮值,制作 jsons,然后将其作为参数传递。所以在控制器中,我将代码编写为-
var answers = '{"data":[';
var i=0;
QbqnsStore.each(function(model){
i++;
var inputs = document.getElementsByName(model.get('questionId'));
console.log(document.getElementsByName(model.get('questionId')));
console.log("length is"+inputs.length);
for (var j = 0; j < inputs.length; j++) {
if (inputs[j].checked) {
console.log("count of store is"+QbqnsStore.count());
//if (i == QbqnsStore.count()){
if (i == QbqnsStore.count()){
console.log("value of i is"+i);
answers = answers + '{"paperId":"'+paperNumber+'","userId":"'+userId+'","questionId":"'+inputs[j].name+'","option":'+inputs[j].value+'}'
//i=1;
}
else{
console.log("value of i is"+i);
answers = answers + '{"paperId":"'+paperNumber+'","userId":"'+userId+'","questionId":"'+inputs[j].name+'","option":'+inputs[j].value+'},'
}
}
}// End of inner for loop
}); //End of each
answers =answers+']}';
console.log("selected data is:");
console.log(answers);
var storeObject=this.getStore('qb.QbquestionoptionStore');
storeObject.load({
params:{
data: answers
},
callback: function(records,operation,success){
},
scope:this
});
当用户解决所有问题时,它可以正常工作。在解决所有问题的情况下,上述函数正在形成 json as-
{"data":[{"paperId":"1517","userId":"116","questionId":"1","option":1},{"paperId":"1517","userId":"116","questionId":"2","option":4},{"paperId":"1517","userId":"116","questionId":"3","option":9},{"paperId":"1517","userId":"116","questionId":"4","option":9}]}
它的格式正确。但是当用户只从纸上解决前一个或两个问题时,它会形成 json as-
{"data":[{"paperId":"1518","userId":"116","questionId":"2","option":4},]}
即在 json 末尾插入额外的逗号,这使得 json 无效。那么当用户只解决一个或两个问题时,如何从 json 中删除这个额外的逗号。请指导我