[英]Javascript to alert a user that the same info has already been entered
First I should say I am a javascript newbie so forgive my ignorance. 首先,我应该说我是一名JavaScript新手,所以请原谅我的无知。 I'm creating a form that has three functions and also uses array: Add - To accept a name (if field left blank it should ask the user to enter a name in an alert box) Find - To verify a name has not already been entered (in an alert box) List - To list the names that have been entered (in an alert box)
我正在创建具有三个功能并且还使用数组的表单:添加-接受名称(如果字段为空,则应要求用户在警报框中输入名称)查找-确认名称尚未被使用输入(在警报框中)列表-列出已输入的名称(在警报框中)
I have the list function working (good). 我有列表功能工作(好)。 The alert to enter a name comes up after you enter a name as well as when you leave the field blank (not good) and I can't get the find function to work at all.
输入名称后以及将字段保留为空白(不好)时,都会出现输入名称的警报,而我根本无法使用查找功能。 My code is below and I've tried so many iterations and searched so many sites for help, also tried firebug;
我的代码在下面,我尝试了许多迭代,并搜索了许多站点以寻求帮助,还尝试了Firebug。 I'm hoping someone can point me in the right direction.
我希望有人可以指出正确的方向。 Untitled
无标题
<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>
The code had a couple of errors, here's a working version: http://jsfiddle.net/sAq2m/2/ 该代码有几个错误,这是一个有效的版本: http : //jsfiddle.net/sAq2m/2/
html: 的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: 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;
}
}
You have done it almost, but some lil errors.. here you can check it jsfiddle 您已经差不多完成了,但是有些小错误。在这里您可以检查它jsfiddle
HTML: 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: 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 = "";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.