Javascript 来提醒用户已经输入了相同的信息
Javascript to alert a user that the same info has already been entered
首先,我应该说我是一个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;
}
}
相关文章:
- 根据CHECKBOX输入值动态更新DIV信息
- 如何循环请求用户输入,直到用户在Acrobatneneneba XI中输入信息
- 在 Jquery ajax 中,在一个字段中输入时获取详细信息
- 如何手动馈送高图表主详细信息图表的输入
- 有关 Javascript IndexedDB 输入信息的问题
- 使用终端将信息输入到Javascript中
- 如何使用html/html5中的用户输入获取完整的地理位置详细信息
- 如何将用户输入的信息从网站直接发送到我的电子邮件中
- 将信息结果返回到多个输入字段
- 如何使用 Cookie 将信息保存在输入标签中
- Javascript 来提醒用户已经输入了相同的信息
- 如何使用 localStorage/Javascript 来记住信息(输入值)
- 如何在 HTML 中输入某些信息来自我的 json
- 如何从输入复选框中获取值以在查询中使用它来显示信息
- 输入最后一个字段后自动提交/刷新信息路径表单
- 使用 Google 地方信息自动完成功能提交 JS 输入
- 我希望用户在未输入所需信息时无法前进到下一个屏幕
- 使用数据表行信息更新文本输入,并从对话框中编辑行
- PHP if 基于输入信息的函数不起作用
- 我试图使用用户通过JavaScript警报框输入的信息在HTML页面上创建一个链接