duanfu3884 2019-04-07 02:15
浏览 45

存储选项值的每个元素的ID值

Can't wrap my head around the issue im having. It's within a group i am currently working with.

Basically, I am being sent a large array. Elements needed to be worried about are the ID number of each question. As well as the question.

I am putting each value of the question in an array, and the ID in an array in order.

When I add the "question" value into the 'options' tag for HTML, I could only add the text (value) of the option selected. Therefore, if I add random questions.. I can't figure out how to attach an ID. The ID is needed to send to my backend so they can find each question much easier.

Goal: To attach a question and an ID array, when I 'select' the question from a drop down and add the value over to a new array to THEN be sent over to backend.. It is a confusing task and I can't seem to figure out a proper way for it to be done.

The best I have done, was to add the ID onto the end of the 'option' as text. But it certainly isnt nice looking, and would have to somehow make a function to take only the number, of the entire string at the end. When i am trying to just send over an ID with the Question String, making it so they can access the question using the unique ID which each one.

<script>

var testArray=[];
var scoreArray=[];

var questionArray=[];
var idArray=[];

var countBox=1;
var boxName=1;


function filterq(){
  var ajaxRequest = new XMLHttpRequest();
  ajaxRequest.onreadystatechange = function() {
    if (this.readyState == 4) {
      document.getElementById('questionSelect').innerText = null
      alert(this.responseText);

      for(i=0;i<21;i++){
      var questionDisplay = document.getElementById("questionSelect");
      var options = document.createElement("option");
      var data=JSON.parse(this.responseText);


      questionArray.push(data['list'][i]['question']);
      idArray.push(data['list'][i]['ID']);

      options.text=questionArray[i]+" (ID:"+idArray[i]+")";
      questionDisplay.add(options);

        }
      }
    }

  var qdiff = document.getElementById("qLevel").value;
  var qtopic = document.getElementById("qTopic").value;
  var qkeyword = document.getElementById("qKeyword").value;

  var myObj = {id: "qfilter", topic: qtopic, difficulty: qdiff, keyword: qkeyword};
  var myJSON = JSON.stringify(myObj);
  ajaxRequest.open("POST","https://web.njit.edu/~rtw3/CS490/betamiddle.php", true);
  //ajaxRequest.open("POST", "fronttest.php", true);
  ajaxRequest.send(myJSON);
}



function addq(){

 //pushing value onto test array
 var addingquestion = document.getElementById('questionSelect').value;
 testArray.push(addingquestion);

 //creating textoutput for each question
 var node = document.createElement("LI");
 var textnode = document.createTextNode(addingquestion);
 node.appendChild(textnode);
 document.getElementById("test").appendChild(node);

 //adding textboxes each click
 var boxName = "q" + countBox;
 document.getElementById('test').innerHTML+='<input type="text" id="'+boxName+'" size="1" placeholder="pts" maxlength="2""  /><br/>';
 countBox+=1;


}

function examAdd(){
  var ajaxRequest = new XMLHttpRequest();
  ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){

      document.getElementById("testLayout").innerHTML = "Exam has been Successfully Created";

        }
    }

  var topics = document.getElementById("qTopic").value;
  var keywords = document.getElementById("qKeyword").value;
  var testname = document.getElementById("eName").value;
  for(i=1; i<countBox; i++){
  var theID = document.getElementById("q"+i).value;
  scoreArray.push(theID);
  }

    var myObj = {id:"addt", test: testArray, scores: scoreArray, topic: topics, keyword: keywords, tname: testname};
  var myJSON = JSON.stringify(myObj);
  var myJSON2 = JSON.stringify(scoreArray);
  //ajaxRequest.open("POST","examtest.php", true);
  ajaxRequest.open("POST","https://web.njit.edu/~rtw3/CS490/betamiddle.php", true);
    ajaxRequest.send(myJSON);
}

function cancel(){
  document.getElementById('test').innerHTML=null;
}


</script>

</body>
</html>

Attaching a unique ID to Every question AFTER it has been added to a new array, to then be sent back with each unique ID and Question in order.

note: the id is already with the question when its sent. I just need to save it somehow when i send it back but im unsure how since i send back the .value of an option selected, rather than an array with questions and IDs.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 steam下载游戏占用内存
    • ¥15 树莓派5怎么用camera module 3啊
    • ¥20 java在应用程序里获取不到扬声器设备
    • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
    • ¥15 Attention is all you need 的代码运行
    • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
    • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
    • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
    • ¥80 部署运行web自动化项目
    • ¥15 腾讯云如何建立同一个项目中物模型之间的联系