简体   繁体   English

如何使用JavaScript添加/检查数组中的值

[英]How to add/check values in array with javascript

I have this html code test.html : 我有这个html代码test.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src="jquery.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script type="text/javascript">
  $(document).ready(function() {
    var course_data;     
    $.get('exerc.xml', function(data) { 
    course_data = data;               
    var that = $('#courses'); 
    $('course', course_data).each(function() {
    $('<option>').text($(this).attr('title')).appendTo(that);
    });
  }, 'xml'); 

 $('#courses').change(function() { 
    var val = $(this).val(); 
    var that = $('#times').empty(); 
    $('course', course_data).filter(function() { 
      return val == $(this).attr('title'); 
    })
    .find("lesson").each(function() { 
      $("#lesson").val($(this).text());   
    });
 });
 });
 </script>

<script type="text/javascript">
function keyPressed(event, input) {
   if (event.keyCode == 8) {
               // Allow backspace
   return true;
  }
              // Detect character code: event.which on Firefox, event.keyCode on IE
   var char = event.which ? event.which : event.keyCode;
             // Convert to string
   char = String.fromCharCode(char);
   var exerc = new Array();
   exerc = "1234 1234 1234";
    // Compare to character in match string and return result
   return (exerc.charAt(input.value.length) == char);
   }
</script>   
</head>

<body>
 <form method="post" action="">
 <input type="text" size="90" id="lesson" />
 </form>
 <form  id="form2" name="form2" method="post" action="">
 <input size="90" type="text" class="textarea" onkeypress="return keyPressed(event, this);" />
 </form>

 <form name="form1">
 <p>exercices
   <select style="width:100px" id='courses'>
   <option selected="selected">choose...</option>
   </select>
 </form> 
 </body>
 </html>

And this is my xml file exerc.xml : 这是我的xml文件exerc.xml

 <?xml version="1.0" encoding="utf-8"?>
 <courses>
    <course title="exercise 1">
        <lesson>1234 1234 1234</lesson>
    </course>
    <course title="exercise 2">
        <lesson>5678 5678 5678</lesson>
    </course>
    <course title="exercise 3">
        <lesson>9012 9012 9012</lesson>
    </course>
 </courses>

Well, there are two inputs fields above in the first code. 好吧,在第一个代码中上面有两个输入字段。 In the first input enter the data from the xml file (parser method). 在第一个输入中,输入来自xml文件的数据(解析器方法)。 This is depend on the choice from dropdown menu. 这取决于从下拉菜单中的选择。 In the second input i use a function (keyPressed) which allow me to enter the same data that depict in the first input, but in order one by one. 在第二个输入中,我使用一个函数(keyPressed),该函数允许我按第一个输入中的顺序输入相同的数据。 This is work very well with the first exercise. 第一次练习时效果很好。 The problem is that i want to work with more exercises. 问题是我想进行更多练习。 I add an array with more exercises like this: 我添加了一个包含更多练习的数组:

 var exerc = new Array();
   exerc[0]= "1234 1234 1234";
   exerc[1] = "5678 5678 5678";
   exerc[2] = "9012 9012 9012"; 

I change the first function keyPressed, but it is doesn't work. 我更改了第一个功能keyPressed,但是它不起作用。 Beybe it is totally wrong: Beybe,这是完全错误的:

 function keyPressed(event, input) {
    if (event.keyCode == 8) {
    return true;
    }
    var char = event.which ? event.which : event.keyCode;
    char = String.fromCharCode(char);
     var exerc = new Array();
       exerc[0]= "1234 1234 1234";
       exerc[1] = "5678 5678 5678";
       exerc[2] = "9012 9012 9012";
      for (i=0;i<exerc.length;i++){
      document.getElementById("courses").selectedIndex;
      }
     return (exers.charAt(input.value.length) == char);
   }

I use this exercise like a game. 我把这项练习当作游戏来使用。 I can add or removes exercises from xml file and I learn the provision of keys. 我可以从xml文件添加或删除练习,并且可以学习密钥的提供。 Maybe it is stupid for many people here, but i found it interesting. 也许对于这里的许多人来说这是愚蠢的,但是我发现它很有趣。 So, i stack here. 所以,我堆在这里。 Any suggestion how to fix it? 有什么建议如何解决吗? Thanks in advance. 提前致谢。

this is the solution, if someone have the same problem! 如果有人遇到同样的问题,这就是解决方案!

var exerc = ["1234 1234 1234","5678 5678 5678","9012 9012 9012"]; 
for (i=0;i<exerc.length;i++){

var option=document.getElementById("courses").selectedIndex;
}

return (exerc[option - 1].charAt(input.value.length) == char);

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

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