以下是我的代碼。這仍在進行中;所以,你會看到一些內容爲空的函數。另外,這是我的第一個Flex應用程序;請多多包涵。如何檢查Flex中動態生成的單選按鈕和複選框上的用戶選擇?
這是一個測驗應用程序,它可以從ColdFusion Web服務獲取每個問題的問題和答案。有三種類型的問題:真或假,單選的多選,以及多選的多選。因此,根據問題類型,應用程序會動態生成適當數量的單選按鈕或複選框供用戶選擇。我得到了這些工作。我遇到的問題是,我不確定如何檢查用戶實際選擇的內容。在其他網站的一些其他論壇和帖子中,它表示我可以使用event.currentTarget.selectedValue來獲取用戶選擇。但是當我真的這樣做時,我得到了一個運行時錯誤,說:「在mx.controls.FormItem上找不到屬性selectedValue,並且沒有默認值。」我的問題是,我需要做什麼來捕獲用戶選擇?
由於提前,
蒙
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationComplete="initVars()">
<fx:Declarations>
<s:RemoteObject id="CFCertService" destination="ColdFusion" source="CFCertExam.cfquiz">
<s:method name="returnQuestions" result="resultHandler(event)"/>
<s:method name="returnAnswers" result="answerHandler(event)"/>
</s:RemoteObject>
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.containers.FormItem;
import mx.controls.Alert;
import mx.controls.CheckBox;
import mx.controls.RadioButton;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.RemoteObject;
[Bindable]
private var questionArray:ArrayCollection;
private var questionType:String;
private var answerItem:FormItem;
[Bindable]
private var currentQuestionCounter:int;
[Bindable]
private var answerArray:ArrayCollection;
private var myOptionButton:RadioButton = new RadioButton();
private var myOptionButton2:RadioButton = new RadioButton();
private function initVars():void {
currentQuestionCounter = 0;
btnPrev.enabled = false;
btnNext.enabled = false;
}
private function answerHandler(event:ResultEvent):void {
answerArray = event.result as ArrayCollection;
var i:int;
answerForm.removeAllChildren();
answerItem = new FormItem();
answerForm.addChild(answerItem);
switch (questionType) {
case "True or False":
{
myOptionButton.label = "True";
if (answerArray.getItemAt(0).Answer_Choice == "True") {
myOptionButton.value = 1;
} else {
myOptionButton.value = 0;
}
answerItem.addChild(myOptionButton);
myOptionButton2.label = "False";
if (answerArray.getItemAt(0).Answer_Choice == "False") {
myOptionButton2.value = 1;
} else {
myOptionButton2.value = 0;
}
answerItem.addChild(myOptionButton2);
answerItem.addEventListener(MouseEvent.CLICK, selectionHandler);
break;
}
case "Multiple Choice (Single Selection)":
{
for (i=0; i<answerArray.length; i++) {
var myOptionButton1:RadioButton = new RadioButton();
myOptionButton1.label = answerArray.getItemAt(i).Answer_Choice;
if (answerArray.getItemAt(i).Corect_Flag == "1") {
myOptionButton1.value = 1;
} else {
myOptionButton1.value = 0;
}
answerItem.addChild(myOptionButton1);
}
break;
}
case "Multiple Choice (Multiple Selection)":
{
for (i=0; i<answerArray.length; i++) {
var myCheckBox:CheckBox = new CheckBox();
myCheckBox.label = answerArray.getItemAt(i).Answer_Choice;
answerItem.addChild(myCheckBox);
}
break;
}
}
answerForm.x = 380;
answerForm.y = 200;
}
private function selectionHandler(event:MouseEvent):void {
Alert.show(event.currentTarget.toString());
}
private function resultHandler(event:ResultEvent):void {
questionArray = event.result as ArrayCollection;
txt1Questions.htmlText = questionArray.getItemAt(currentQuestionCounter).Question_Text;
questionType = questionArray.getItemAt(currentQuestionCounter).Question_Type;
btnNext.enabled = true;
CFCertService.returnAnswers(questionArray.getItemAt(currentQuestionCounter).Question_ID);
}
private function buttonEventHandler():void {
CFCertService.returnQuestions();
btnStartExam.enabled = false;
}
private function btnPrevEventHandler():void {
currentQuestionCounter--;
if (currentQuestionCounter == 0) {
btnPrev.enabled = false;
}
if (currentQuestionCounter < questionArray.length) {
btnNext.enabled = true;
}
txt1Questions.htmlText = questionArray.getItemAt(currentQuestionCounter).Question_Text;
questionType = questionArray.getItemAt(currentQuestionCounter).Question_Type;
CFCertService.returnAnswers(questionArray.getItemAt(currentQuestionCounter).Question_ID);
}
private function answerReturnHandler(questionIndex:int):void {
}
private function btnNextEventHandler():void {
currentQuestionCounter++;
if (currentQuestionCounter > 0) {
btnPrev.enabled = true;
}
if (currentQuestionCounter >= (questionArray.length - 1)) {
btnNext.enabled = false;
}
txt1Questions.htmlText = questionArray.getItemAt(currentQuestionCounter).Question_Text;
questionType = questionArray.getItemAt(currentQuestionCounter).Question_Type;
CFCertService.returnAnswers(questionArray.getItemAt(currentQuestionCounter).Question_ID);
}
]]>
</fx:Script>
<mx:Text id="txt1Questions" x="129" y="124"/>
<s:Button id="btnStartExam" label="Start Exam" click="buttonEventHandler()" x="370" y="54"/>
<mx:Form id="answerForm"/>
<s:Button x="129" y="436" label="Previous" id="btnPrev" click="btnPrevEventHandler()" enabled="false"/>
<s:Button x="642" y="436" label="Next" id="btnNext" click="btnNextEventHandler()" enabled="false"/>
</s:Application>
您應該始終將代碼格式化爲代碼,否則XML標籤根本不會顯示。使用{}按鈕或縮進4個空格。而且,只是粘貼你的整個程序很可能無助於獲得答案。將問題縮小到與問題相關的那些代碼行 - 沒有人想要重新創建軟件來理解你所要求的內容。 – weltraumpirat 2011-01-27 17:03:35
我點擊了代碼示例圖標,然後我在那裏粘貼了代碼。我應該點擊其他圖標嗎?就粘貼整個程序而言,已經很晚了,我沒有想到。對於那個很抱歉。 – 2011-01-27 23:08:21