简体   繁体   English

ng-checked单选按钮

[英]ng-checked in radio button

I'm making a quiz app.To show the progress of the quiz there is question pallette of small boxes which turn yellow when users click on any of the the radio button to show that the user has attempted the question.Along with this there is a button to move to the previous question.To show the users initial answer I have used ng-checked directive with some logic in the controller.Everything is working fine but after attempting a question when I move to the next question and click on the same option as the previous question then the question pallette box does not turn yellow.But when I click on the other option it works fine. 我正在制作一个测验应用程序,为了显示测验的进度,有一个小盒子的问题托盘,当用户单击任何单选按钮以表明用户已经尝试了问题时,这些盒子会变成黄色。按钮以移至上一个问题。为显示用户的初始答案,我在控制器中使用了ng-checked指令并带有一些逻辑。一切正常,但尝试输入一个问题后,我移至下一个问题并单击相同的按钮选项作为上一个问题,则问题调色板框不会变为黄色。但是当我单击另一个选项时,它可以正常工作。

.html html的

<div  class="questionsBox" >
        <div class="questions">{{liveCtrl.questions[liveCtrl.activeQuestion].question}}</div>
        <ul class="answerList">
            <li>
                <label>
                    <input data-id="{{liveCtrl.activeQuestion}}" type="radio" ng-checked="liveCtrl.useranswers[liveCtrl.activeQuestion].q===1" ng-click="liveCtrl.answers(liveCtrl.activeQuestion,1)" name="answerGroup" value="0" >&nbsp;{{liveCtrl.questions[liveCtrl.activeQuestion].optionA}}</label>
            </li>
            <li>
                <label>
                    <input data-id="{{liveCtrl.activeQuestion}}" type="radio" ng-checked="liveCtrl.useranswers[liveCtrl.activeQuestion].q===2" ng-click="liveCtrl.answers(liveCtrl.activeQuestion,2)" name="answerGroup" value="1" >&nbsp;{{liveCtrl.questions[liveCtrl.activeQuestion].optionB}}</label>
            </li>
            <li>
                <label>
                    <input data-id="{{liveCtrl.activeQuestion}}" type="radio" ng-checked="liveCtrl.useranswers[liveCtrl.activeQuestion].q===3"  ng-click="liveCtrl.answers(liveCtrl.activeQuestion,3)" name="answerGroup" value="2" >&nbsp; {{liveCtrl.questions[liveCtrl.activeQuestion].optionC}}</label>
            </li>
            <li>
                <label>
                    <input data-id="{{liveCtrl.activeQuestion}}" type="radio" ng-checked="liveCtrl.useranswers[liveCtrl.activeQuestion].q===4"  ng-click="liveCtrl.answers(liveCtrl.activeQuestion,4)" name="answerGroup" value="3" >&nbsp; {{liveCtrl.questions[liveCtrl.activeQuestion].optionD}}</label>
            </li>
        </ul>
        <div class="questionsRow">
                <button  ng-disabled="liveCtrl.questions.length==liveCtrl.activeQuestion+1" class="subques btn btn-lg btn-secondary"  ng-click="liveCtrl.nextQuestion()">Save & Next</button>
                <button  ng-click="liveCtrl.prevQuestion()" ng-disabled="liveCtrl.activeQuestion == 0" class="subques btn btn-lg btn-secondary" >Previous</button>
        </div>             
    </div>

//Question Pallete //问题托盘

 <div class="question-pallete">
    <div  id="{{$index}}"  ng-repeat="question in liveCtrl.questions" class="square">
       <a ng-click="liveCtrl.gotoQues($index)">
          {{$index + 1}}
        </a>
</div>

//jquery to give color to the boxes // jquery为框赋予颜色

<script type="text/javascript">
    $(document).on('click',"input[name='answerGroup']",function(){
        var qid=$(this).data('id');
        console.log(qid);
        $('#'+qid).addClass('box-color');
    });

</script>

Controller functions 控制器功能

this.nextQuestion=()=>{
    live.activeQuestion++;
    //console.log(live.activeQuestion);
};
this.prevQuestion=()=>{
    live.activeQuestion--;
    //console.log(live.activeQuestion);
};
this.gotoQues=(qno)=>{
    live.activeQuestion=qno;
}
this.answers=(qid,option)=>{
    //console.log(qid);
    live.useranswers[qid]={
        q:option

};

When I'm tried to console qid in jquery part it outputs the same qid for the same option in the next question but it is not the case for other options.I think "data-id" in html is not updating for that case.Sorry If I was not able to explain it properly. 当我尝试在jquery部分中控制qid时,它将在下一个问题中为相同选项输出相同的qid,但对于其他选项则不是这样。我认为html中的“ data-id”不会针对这种情况进行更新。抱歉,如果我无法正确解释。

I find 2 issues with your implementation. 我发现您的实施存在2个问题。

  1. I don't see ng-model in any of your input field. 在您的任何输入字段中都看不到ng-model
  2. Why don't you use ng-class instead of using jquery to get the id and add class? 为什么不使用ng-class而不是使用jquery获取ID和添加类?

     <label ng-class="val==0 ? 'highlight':''"> <input type="Radio" ng-model="val" ng-value="0">Option A</label> 

Here is the jsfiddle link 这是jsfiddle链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM