我在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为 -
{"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 -
{"data":[{"paperId":"1518","userId":"116","questionId":"2","option":4},]}
即。在json的末尾插入额外的逗号,这使得json无效。所以当用户只解决一个或两个问题时,如何从json中删除这个额外的逗号。请指导我
答案 0 :(得分:0)
我认为你的情况永远不会成真。你试过了吗?
if (i == QbqnsStore.count() - 1){
而不是
if (i == QbqnsStore.count()){