用于提醒用户已输入相同信息的Javascript

时间:2013-11-02 18:40:06

标签: javascript arrays function

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

我的列表功能正常(好)。输入名称后以及将字段留空时(不好),会出现输入名称的提示 我根本无法使用find功能。 我的代码在下面,我已经尝试了这么多次迭代,搜索了很多网站寻求帮助,也尝试过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>

2 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:0)

你几乎已经完成了,但是有些错误..在这里你可以检查它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 = "";
}