【Angular2】CheckBox獲取值的兩種方式
前言
因為專案中需要用到CheckBox向後臺提交資料
最開始設定的是學生選擇什麼就直接把答案合成一個字串傳到後端
但是需要回顯學生答案,後端返回的json資料中答案是一個處理後的字串,無法進行判斷
所以採用陣列形式答案,可以在HTML中利用模板表示式進行迴圈遍歷判斷,從而實現答案回顯的功能
方式一 _ 答案合成字串
html code
<div>
<p>{{question.questionMainOrder + ". " + question.questionContent}}</p>
<div *ngFor="let option of question.questionSubList;let i=index" >
<input type="checkbox" name="{{option.questionMainId}}" value="{{option.optionsContent}}" id="{{option.optionOrder}}"
(click)="updateAnswer($event.target,$event.target.checked, $event.target.value)">
{{alphabet[i]}}{{'. '+option.optionsContent}}
</div>
</div>
ts code
private options: Array<string> = [];//使用者的答案選項(在方法外宣告,否則本題剛選擇答案會消失)
updateAnswer(el: Element, checked: boolean, value: string) {
//獲得使用者的答案
var index: number = this.options.indexOf(value);
if (checked) {
if (index < 0) {
this.options.push(value+"|");
}
} else {
if (index > -1) {
this.options = this.options.filter((ele, index) => {
return ele != value;
})
}
}
//將答案合成字串,用分隔符分開
let strInput: string = "";
for (var i = 0; i < this.question.questionSubList.length; i++) {
if (this.inputs[i] == "" || this.inputs.length < this.question.questionSubList.length) { answer.done = false; }
if (this.inputs[i] != null) {
strInput = strInput + this.inputs[i] + "|";
} else {
strInput = strInput + "|";
}
}
//處理使用者答案
if (this.options.length == 0) {
answer.studentAnswer = "";
answer.done = false;
} else {
let strChecked: string = "";
this.options.forEach((val, idx, array) => {
strChecked = strChecked + val;
});
answer.studentAnswer = strChecked;
answer.done = true;
}
//傳遞使用者的答案
this.updatedAnswer.emit(answer);
this.updateStudentAnswerToBackend(answer);
}
方式二 _ 答案轉為陣列
html code
<div>
<p>{{question.questionMainOrder + ". " + question.questionContent}}</p>
<div *ngFor="let option of question.questionSubList;let i=index">
<input type="checkbox" name="{{option.questionMainId}}" value="{{option.optionsContent}}" id="{{option.optionOrder}}"
checked="{{option.optionsContent==question.studentAnswerArray[i] ? 'checked' : ''}}"
(click)="updateAnswer($event.target,$event.target.checked, $event.target.value)">
{{alphabet[i]}}{{'. '+option.optionsContent}}
</div>
</div>
ts code
private options: Array<string> = [];//使用者的答案輸入(在方法外宣告,否則本題剛選擇答案會消失)
updateAnswer(el: Element, checked: boolean, value: string) {
//使用者的答案(在方法內宣告,每次提交為新答案實體,simplechange才會識別)
let answer: Answer = new Answer;
//宣告陣列
answer.studentAnswerArray=[];
//獲得題目id
let questionid: string = el.getAttribute("name");
//獲得選項id
let optionid:number =Number(el.getAttribute("id"));
if(checked==true){
this.options[optionid-1]=value;
}else{
this.options[optionid-1]="";
}
//處理選項結果
for (var i = 0; i < this.question.questionSubList.length; i++) {
if(this.options[i]=="" || this.options[i]==null){
this.options[i]=""
}
}
//處理使用者答案
answer.studentId=localStorage.getItem("studentCode");
answer.paperId=this.paperId;
answer.questionTypeId=this.questionTypeId;
answer.questionMainId=questionid;
answer.studentAnswerArray=this.options;
answer.done=false;
for(var i=0;i<this.options.length;i++){
if(this.options[i]=="" || this.options[i]==null){
}else{
answer.done=true;
}
}
console.log("question-checkbox-answer--"+answer);
//傳遞使用者的答案
this.updatedAnswer.emit(answer);
this.updateStudentAnswerToBackend(answer);
}
小結
根據不同的需求採用不同的實現,需要對開發語言有一個很好的瞭解
這樣遇到問題才會提出一些解決方案,前後端共同完善
最後,儘可能以最小的代價做修改
相關推薦
【Angular2】CheckBox獲取值的兩種方式
前言 因為專案中需要用到CheckBox向後臺提交資料 最開始設定的是學生選擇什麼就直接把答案合成一個字串傳到後端 但是需要回顯學生答案,後端返回的json資料中答案是一個處理後的字串,無法進行判
9.10 路由控制之反向解析--【別名】html和views兩種方式實現
com 反向 技術分享 http mage login gin bsp 分享圖片 1. 在html裏反向解析 給路徑起別名,修改路徑時,不用每個地方都修改。 {% url ‘Log‘ %} : 就會去找別名為Log的URL,找到 "login/"後把"logi
【SSM】檔案下載的兩種方式
檔案下載的兩種方式 第一種:檔名和大小都是固定的 第二種:檔案有很多,檔名不固定 今天專案中用到了一個檔案下載的方式,將一個app放在伺服器上,可以直接下載,剛開始想到了使用輸出檔案流的方式,思路有了,就直接上手寫了,參考網上的資料,整理出來了一
【模板】並查集 兩種路徑壓縮寫法
let con while class 模板 union 實踐 return ren class UnionFind{ private: int* parent; int* rank; int count; public: UnionFin
【譯】統計建模:兩種文化(第六部分)
謝絕任何不通知本人的轉載,尤其是抄襲。 Abstract 1. Introduction 2. ROAD MAP 3. Projects in consulting 4. Return to the university 5. The
【譯】統計建模:兩種文化(第四、五部分)
謝絕任何不通知本人的轉載,尤其是抄襲。 Abstract 1. Introduction 2. ROAD MAP 3. Projects in consulting 4. Return to the university 5. The
【譯】統計建模:兩種文化(第三部分)
謝絕任何不通知本人的轉載,尤其是抄襲。 Abstract 1. Introduction 2. ROAD MAP 3. Projects in consulting 4. Return to the university 5. The
【Python】 import語句的兩種匯入方法
匯入指定模組中的指定函式: from datetime import datetime ** 僅訪問模組提供的功能,不向程式程式碼中匯入明確指定的函式 ** import time 要訪問以這種方式匯入的一個模組提供的函式,可以用點記法語法來指定 time.
【模板】連結串列的兩種實現形式
摘自李煜東《演算法競賽進階指南》 //連結串列模板1 struct Node{ int value;//資料 Node *prev,*next;//指標 }; Node *head,*tail; void initialize()//建
【tensorflow】檔案佇列的兩種建立和載入方式
方式1:記憶體載入 ①建立佇列: queue=tf.FIFOQueue(...) or queue=tf.RandomShuffleQueue(...) ②新增enqueue op: enqueue=tf.enqueue(xxx) or enqueue=tf.enqueue
【Java】類賦值的使用方式
region pre tin image 直接 beanutils 期望 http span Regioninfo regioninfo=new Regioninfo(); regioninfo.setDistance(0);
【Linux】安裝軟體的三種方式--rpm、yum、原始碼包
說明:此文首發於本人原創公眾號【媛測】中,如有轉載,請著明出處! 在windows系統下安裝軟體很簡單,下載軟體包後雙擊.exe檔案,點選下一步即可。然而在linux安裝軟體就沒那麼容易了,不同的Linux發行版有所差異,下面以centos7系統為例說明,如何在linux下安裝軟體
【dubbo】服務執行的三種方式
dubbo服務的執行方式有三種,分別為: 1. 使用Servlet容器執行(Tomcat、Jetty等) 2. 自建Main方法類來執行(Spring容器) 3. 使用Dubbo框架提供的Main方法類來執行
【框架】[Spring]AOP攔截-三種方式實現自動代理
這裡的自動代理,我講的是自動代理bean物件,其實就是在xml中讓我們不用配置代理工廠,也就是不用配置class為org.springframework.aop.framework.ProxyFactoryBean的bean。 總結了一下自己目前所學的知識
【eclipse】myeclipse10安裝外掛三種方式
本文講解MyEclipse(MyEclipse10)的三種方法,以SVN為例 一、通過MyEclipse Configuration Center線上安裝 1. 開啟MyEclipse10,在選單欄選擇MyEclipse→MyEclipse Co
【ITOO】WCF宿主的幾種方式
上篇部落格已經說明了WCF的一個服務如何建立,那麼建立好了服務之後,我們如何釋出出去呢?要想完整的做完一個WCF服務,一共分三步走,第一步就是像上篇部落格所說建立服務,第二步是釋出服務,還有最後一步就
【Jquery】jQuery獲取URL參數的兩種方法
ont ras mil scrip line 兩種方法 lower quest request jQuery獲取URL參數的關鍵是獲取到URL,然後對URL進行過濾處理,取出參數。 location.href是取得URL。location.search是取得URL“?
【Linux】自動獲取Ip的兩種方法
在安裝好Centos系統後,每次登陸上之後都需要通過手動方式進行網路連線,過於麻煩,因此改成自動連線的方式。 &nb
【Jquery】jQuery獲取URL引數的兩種方法
jQuery獲取URL引數的關鍵是獲取到URL,然後對URL進行過濾處理,取出引數。location.href是取得URL,location.search是取得URL“?”之後的字串,也就是說引數部
【REACT NATIVE 系列教程之十三】利用LISTVIEW與TEXTINPUT製作聊天/對話方塊&&獲取元件例項常用的兩種方式
補充說明:一:很多童鞋問,鍵盤調出來被擋住了,那麼下面給出三個解決方案:1. 在render最外層包一個ScrollView,然後當鍵盤調出時,scrollTo即可實現。2. 在底部新增一個可變化高度的view,根據鍵盤獲取、失去焦點時,進行處理實現二:有的童鞋說對話方塊的背景沒有根據內容長短自適應,OK ,