簡體   English   中英

聯系人保存結果未顯示在Javascript中

[英]Contact saving result is not showing in 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 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);
}

注意:應該define these variables globally以便在使用時可以使用它們。

這里的問題是變量范圍。

嘗試這個:

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中,變量僅在特定范圍內聲明,該范圍對於聲明它們的函數而言是全局的或局部的。 既然你宣布nam1num1searc功能他們沒有可用的外面。

看一下您的錯誤控制台。 通常,至少在嚴格模式下,您應該得到ReferenceError 為了防止這種情況,請在腳本開頭聲明變量, 不要在函數中重新聲明它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM