联系人保存结果未在Javascript中显示
我用JavaScript编写了一个电话号码保护程序。一切正常,但在搜索框中搜索姓名或号码时,不会显示任何结果:联系人保存结果未在Javascript中显示,javascript,Javascript,我用JavaScript编写了一个电话号码保护程序。一切正常,但在搜索框中搜索姓名或号码时,不会显示任何结果: function contact() { var nam1=prompt("Please enter the name"); var num1=prompt("please enter the phone number"); } contact(); function search() { var searc= prompt("Please enter th
function contact() {
var nam1=prompt("Please enter the name");
var num1=prompt("please enter the phone number");
}
contact();
function search() {
var searc= prompt("Please enter the name of your contact or phone number");
}
search();
//search box
if ( searc == nam1 ) {
alert("The phone Number is , " + num1);
}
if ( searc == num1 ) {
alert("The Contact Name is , " + nam1);
}
试试这个:
var nam1='';
var num1='';
var searc='';
function contact() {
nam1=prompt("Please enter the name");
num1=prompt("please enter the phone number");
}
contact();
function search() {
searc= prompt("Please enter the name of your contact or phone number");
}
search();
//search box
if ( searc == nam1 ) {
alert("The phone Number is , " + num1);
}
if ( searc == num1 ) {
alert("The Contact Name is , " + nam1);
}
var nam1;
var num1;
var searc;
function contact() {
nam1 = prompt("Please enter the name");
num1 = prompt("please enter the phone number");
}
contact();
function search() {
searc = prompt("Please enter the name of your contact or phone number");
}
search();
//search box
if ( searc == nam1 ) {
alert("The phone Number is , " + num1);
}
if ( searc == num1 ) {
alert("The Contact Name is , " + nam1);
}
注意:您应该全局定义这些变量
,以便在需要时可以使用这些变量
使用。尝试以下操作:
var nam1='';
var num1='';
var searc='';
function contact() {
nam1=prompt("Please enter the name");
num1=prompt("please enter the phone number");
}
contact();
function search() {
searc= prompt("Please enter the name of your contact or phone number");
}
search();
//search box
if ( searc == nam1 ) {
alert("The phone Number is , " + num1);
}
if ( searc == num1 ) {
alert("The Contact Name is , " + nam1);
}
var nam1;
var num1;
var searc;
function contact() {
nam1 = prompt("Please enter the name");
num1 = prompt("please enter the phone number");
}
contact();
function search() {
searc = prompt("Please enter the name of your contact or phone number");
}
search();
//search box
if ( searc == nam1 ) {
alert("The phone Number is , " + num1);
}
if ( searc == num1 ) {
alert("The Contact Name is , " + nam1);
}
注意:您应该全局定义这些变量
,以便在需要时可以使用这些变量
使用。这里的问题是变量的作用域 试试这个:
var nam1='';
var num1='';
var searc='';
function contact() {
nam1=prompt("Please enter the name");
num1=prompt("please enter the phone number");
}
contact();
function search() {
searc= prompt("Please enter the name of your contact or phone number");
}
search();
//search box
if ( searc == nam1 ) {
alert("The phone Number is , " + num1);
}
if ( searc == num1 ) {
alert("The Contact Name is , " + nam1);
}
var nam1;
var num1;
var searc;
function contact() {
nam1 = prompt("Please enter the name");
num1 = prompt("please enter the phone number");
}
contact();
function search() {
searc = prompt("Please enter the name of your contact or phone number");
}
search();
//search box
if ( searc == nam1 ) {
alert("The phone Number is , " + num1);
}
if ( searc == num1 ) {
alert("The Contact Name is , " + nam1);
}
这里的问题是变量的作用域 试试这个:
var nam1='';
var num1='';
var searc='';
function contact() {
nam1=prompt("Please enter the name");
num1=prompt("please enter the phone number");
}
contact();
function search() {
searc= prompt("Please enter the name of your contact or phone number");
}
search();
//search box
if ( searc == nam1 ) {
alert("The phone Number is , " + num1);
}
if ( searc == num1 ) {
alert("The Contact Name is , " + nam1);
}
var nam1;
var num1;
var searc;
function contact() {
nam1 = prompt("Please enter the name");
num1 = prompt("please enter the phone number");
}
contact();
function search() {
searc = prompt("Please enter the name of your contact or phone number");
}
search();
//search box
if ( searc == nam1 ) {
alert("The phone Number is , " + num1);
}
if ( searc == num1 ) {
alert("The Contact Name is , " + nam1);
}
在JavaScript中,变量仅是声明它们的函数的全局变量或局部变量。由于您在函数中声明了
nam1
、num1
和searc
,因此它们在外部不可用
请查看您的错误控制台。通常,至少在严格模式下,您应该得到一个
引用错误。为了防止出现这种情况,请在脚本开始时声明变量,不要在函数中重新声明它们。在JavaScript中,变量仅是声明它们的函数的全局变量或局部变量。由于您在函数中声明了nam1
、num1
和searc
,因此它们在外部不可用
请查看您的错误控制台。通常,至少在严格模式下,您应该得到一个引用错误。为了防止出现这种情况,请在脚本开始时声明变量,不要在函数中重新声明它们。@MISJHA您复制了我的答案?@MISJHA您复制了我的答案?