0

首先我应该说我是一个javascript新手,所以请原谅我的无知。我正在创建一个具有三个功能并且还使用数组的表单: 添加 - 接受名称(如果字段留空,它应该要求用户在警报框中输入名称) 查找 - 验证名称尚未输入(在警告框中)列表 - 列出已输入的名称(在警告框中)

我有列表功能工作(好)。输入名称后以及将字段留空(不好)时会出现输入名称的警报,我根本无法使用查找功能。我的代码在下面,我尝试了很多次迭代并搜索了很多网站寻求帮助,还尝试了 firebug;我希望有人能指出我正确的方向。无标题

<body>
<script type="text/javascript">
  var a = new Array();

  function list() {
      var s = "";
      for (i = 0; i < a.length; i++)
          s = s + a[i] + "\n";
      alert(s);
  }

  function add() {
      // If the text box empty you ask the user to enter a name.
      var myTextField = document.getElementById("myText");
      a[a.length] = myTextField.value;
      myTextField.value = "";
      if (myTextField.value == "") {
          alert("Please enter a name");
          return false;
      }

      function find() {
          //If the name already exists you should get a warning
          var myTextField = document.getElementById("myText");
          a[a.length] = myTextField.value;
          myTextField.value = "";
          for (var i = 0; i < a.length; i++)
              if (a[i] == myTextField) {
                  alert("Sorry, the name " + a[i] + " already exists.  Try again");
              }
      }
  }

</script>
<input type="text" id="myText" /><br>
<input type="button" onclick="add()" value="Add a name" />
<input type="button" onclick="list()" value="List the names" />
<input type="button" onclick="find()" value="Find" />
</body>
</html>
4

2 回答 2

0

该代码有几个错误,这是一个工作版本:http: //jsfiddle.net/sAq2m/2/

html:

<input type="text" id="myText" /><br>
<input type="button" onclick="add()" value="Add a name" />
<input type="button" onclick="listItems()" value="List the names" />
<input type="button" onclick="find()" value="Find" />

js:

 var a = [];
  function listItems()
 {
  var s = "";
  for(var i = 0; i < a.length; i++)
  s = s + a[i] + "\n";
  alert(s);
     return false;
 }

 function add()
{
   // If the text box empty you ask the user to enter a name.
 var myTextField = document.getElementById("myText");
    var v = myTextField.value
    if (!v){
        v = prompt("You have not entered a name, please enter one.");
    } 
    a.push(v);

    console.log(a);
  myTextField.value = "";      

 return false;
}
 function find()
{
 //If the name already exists you should get a warning
 var myTextField = document.getElementById("myText");

 for (var i = 0; i < a.length; i++)
    if (a[i] == myTextField.value)
    { 
        alert ("Sorry, the name " + a[i] + " already exists.  Try again");
        return;
    }
}
于 2013-11-02T19:04:31.140 回答
0

你几乎已经完成了,但是有一些 lil 错误.. 在这里你可以检查一下jsfiddle

HTML:

<input type="text" id="myText" /><br>
<input type="button" value="Add a name" class="add_button"/>
<input type="button" value="List the names" class="list_button"/>
<input type="button" value="Find" class="find_button"/>

JS:

$(".add_button").live("click", function(){
    add()
});

$(".list_button").live("click", function(){
    list()
});

$(".find_button").live("click", function(){
    find()
});


var a = new Array();
function list()
{
    var s = "";
    for(i = 0; i < a.length; i++)
    s = s + a[i] + "\n";
    alert(s);
 }

function add()
{
   // If the text box empty you ask the user to enter a name.
    var myTextField = document.getElementById("myText");
    a[a.length] = myTextField.value;

    if (myTextField.value == "")
    {
        alert ("Please enter a name");
        return false;
    }
    myTextField.value = "";
}

function find()
{
    //If the name already exists you should get a warning
    var status = true;
    var myTextField = document.getElementById("myText");

    for (var i = 0; i < a.length; i++)
    {
        if (a[i] == myTextField.value)
        {
            alert ("Sorry, the name " + a[i] + " already exists.  Try again");
            status = false;
            break;
        }
    }
    if(status==true)
    {
        a[a.length] = myTextField.value;
    }
    myTextField.value = "";
}
于 2013-11-02T19:30:27.910 回答