in app have objects map associative array, i'm getting json. map contains questions , 4 answers each question. next i'm trying append question question div , answers radio buttons div answers. seems working without 1 thing. after appending content divs page reloading. question how prevent page reloading in case. tried using ajax call, unfortunately failed. code given below:
<script> $(document).ready(function(){ var questions = []; $.getjson("/exam/json", function (result) { var map = {}; var response = result; (var = 0, l = response.length; < l; i++) { map[response[i].id] = response[i]; } $('#nextbutton').click(function(){ preparequestion(map); }); }); }); function preparequestion(questionlist){ var actualquestionindex = string(math.floor(math.random() * object.keys(questionlist).length) + 1); $('#question').append(questionlist[actualquestionindex].question); if(questionlist[actualquestionindex].ansc === null){ $('#answers').append("<input type='radio' name='ansa' value='a'>"+questionlist[actualquestionindex].ansa+"<br>"); $('#answers').append("<input type='radio' name='ansb' value='b'>"+questionlist[actualquestionindex].ansb+"<br>"); }else{ $('#answers').append("<input type='radio' name='ansa' value='a'>"+questionlist[actualquestionindex].ansa+"<br>"); $('#answers').append("<input type='radio' name='ansb' value='b'>"+questionlist[actualquestionindex].ansb+"<br>"); $('#answers').append("<input type='radio' name='ansc' value='c'>"+questionlist[actualquestionindex].ansc+"<br>"); $('#answers').append("<input type='radio' name='ansd' value='d'>"+questionlist[actualquestionindex].ansd+"<br>"); } delete map[actualquestionindex]; } </script>
i believe button click propagating. try this:
$('#nextbutton').click(function(event){ event.stoppropagation() preparequestion(map); });
for reference please read link event propagation.
Comments
Post a Comment