当用户在提示框中输入IP地址时,如何将IP地址存储到变量。

时间:2016-03-10 09:19:33

标签: javascript phonegap-plugins

我是phoneGap的新手,我在我的代码中使用了navigator.notification.prompt。我想在提示框中添加IP地址(192.168.1.201),添加的IP地址应保存到新变量,并检查该变量是否为空。这是我的代码:

function config()
{    
    var ret =navigator.notification.prompt("Server Address : ",click me,"Server IP",["Ok","Cancel"],"");

    if (ret =='')
    {
        navigator.notification.alert("Plz enter the correct Ip Address");   
    }
    else
    {
        navigator.notification.alert("Ip address is saved for your mobile"
            localStorage.setItem("ipAddress",ret);
    }
}

function clickme()
{
    navigator.notification.alert("Ip address is saved for your mobile", null, "Server IP", "OK");
} 

但是我无法将输入的IP地址保存到变量返回并检查该变量是否为null。请帮我解决这个问题

2 个答案:

答案 0 :(得分:0)

你应该按照以下方式使用提示......

function config() {
        navigator.notification.prompt(
            'Server Address : ',  // message
            onPrompt,                  // callback to invoke
            'Server IP',            // title
            ['Ok','cancel']              // buttonLabels
        );
    }

function onPrompt(results) {
        if (results.input1 =='')
    {
        navigator.notification.alert("Plz enter the correct Ip Address");   
    }
    else
    {
        navigator.notification.alert("Ip address is saved for your mobile");
            localStorage.setItem("ipAddress",results.input1);
    }
    }

如果你想获得ip地址

function getIp(){
 localStorage.getItem("ipAddress");
}

答案 1 :(得分:0)

验证完整答案

function config() {
        navigator.notification.prompt(
            'Server Address : ',  // message
            onPrompt,                  // callback to invoke
            'Server IP',            // title
            ['Ok','cancel']              // buttonLabels
        );
    }

function onPrompt(results) {
        if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?‌​)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)‌​$/.test(results.input1)) 
    {
        navigator.notification.alert("Plz enter the valid Ip Address");   
    }
    else
    {
        navigator.notification.alert("Ip address is saved for your mobile");
            localStorage.setItem("ipAddress",results.input1);
    }
    }