Javascript 来提醒用户已经输入了相同的信息

Javascript to alert a user that the same info has already been entered

本文关键字:信息 输入 用户 Javascript      更新时间:2023-09-26

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

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

<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>

你几乎已经完成了,但是一些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 = "";
}

代码有几个错误,这是一个工作版本: 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;
    }
}