【问题标题】:JavaScript not working in Cordova Visual Studio 2015JavaScript 在 Cordova Visual Studio 2015 中不起作用
【发布时间】:2017-06-07 15:51:16
【问题描述】:

我正在开发一个混合应用程序,创建了 html 页面和 css,在 html 中我包含了 JavaScript,但简单的警报消息也不起作用,并且在 javascript (index.js) 中创建了验证功能,请帮助我。

var attempt = 3; // Variable to count number of attempts.
// Below function Executes on click of login button.
function validate() {
    var username = document.getElementById("username").value;
    var password = document.getElementById("password").value;
    if (username == "abcdef" && password == "abc#123") {
        alert("Login successfully");
        window.location = "success.html"; // Redirecting to other page.
        return false;
    }
    else {
        attempt--;// Decrementing by one.
        alert("You have left " + attempt + " attempt;");
        // Disabling fields after 3 attempts.
        if (attempt == 0) {
            document.getElementById("username").disabled = true;
            document.getElementById("password").disabled = true;
            document.getElementById("submit").disabled = true;
            return false;
        }
    }
}
/* Below line is used for online Google font */
@import url(https://fonts.googleapis.com/css?family=Raleway);
body{
    background :#f2ca78;
}
h2{
/*/background-color: #FEFFED;*/
padding: 30px 35px;
margin: -10px -50px;
text-align:center;
border-radius: 10px 10px 0 0;
}
hr{
margin: 10px -50px;
border: 0;
border-top: 1px solid #ccc;
margin-bottom: 40px;
}
/*div.container{
width: 900px;
height: 610px;
margin:35px auto;
font-family: 'Raleway', sans-serif;
}*/

div.main{
background-color: #f8f2f2;
width: 230px;
padding: 10px 50px 25px;
border: 2px solid gray;
border-radius: 10px;
font-family: raleway;
float:left;
margin-top:50px;
}
input[type=text],input[type=password]{
width: 100%;
height: 40px;
padding: 5px;
margin-bottom: 25px;
margin-top: 5px;
border: 2px solid #ccc;
color: #4f4f4f;
font-size: 16px;
border-radius: 5px;
}
label{
color: #464646;
text-shadow: 0 1px 0 #fff;
font-size: 14px;
font-weight: bold;
}
center{
font-size:32px;
}
.note{
color:red;
}
.valid{
color:green;
}
.back{
text-decoration: none;
border: 1px solid rgb(0, 143, 255);
background-color: rgb(0, 214, 255);
padding: 3px 20px;
border-radius: 2px;
color: black;
}
input[type=button]{
font-size: 16px;
background: linear-gradient(#ffbc00 5%, #ffdd7f 100%);
border: 1px solid #e5a900;
color: #4E4D4B;
font-weight: bold;
cursor: pointer;
width: 100%;
border-radius: 5px;
padding: 10px 0;
outline:none;
}
input[type=button]:hover{
background: linear-gradient(#ffdd7f 5%, #ffbc00 100%);
}
<!DOCTYPE html>
<html>
    <head>
    <!--
        Customize the content security policy in the meta tag below as needed. Add 'unsafe-inline' to default-src to enable inline JavaScript.
        For details, see http://go.microsoft.com/fwlink/?LinkID=617521
    -->
        <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
        
        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
       <!-- <link rel="stylesheet" type="text/css" href="css/index.css">-->
        <link rel="stylesheet" href="css/style.css" />
        <script typ="test/javascript" src="js/login.js"></script>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="scripts/platformOverrides.js"></script>
        <script type="text/javascript" src="scripts/index.js"></script>
        <title>BlankCordovaApp1</title>

</head>
    <body bgcolor="pink">
        <h1 style="color:black;text-align:center"> HEALTH APP</h1>
        <!--<div class="container">-->
        <div class="main">
            <h2>Welcome</h2>
            <form id="form_id" method="post" name="myform">
                <label>User Name :</label>
                <input type="text" name="username" id="username" />
                <label>Password :</label>
                <input type="password" name="password" id="password" />
                <button type="button" value="Login" id="submit" onclick="validate()" />
            </form>
            <span><b class="note">Note : </b>For this demo use following username and password. <br /><b class="valid">User Name : abcdef<br />Password : abc#123</b></span>
            </div>
    </body>
</html>

【问题讨论】:

  • 你的按钮标签格式不正确,应该是这样的:&lt;button type="button" id="submit" onclick="validate()" &gt;Login&lt;/button&gt;
  • 这个我也试过了,还是不行
  • onclick 验证功能,或任何简单的 javascrpit 也无法在科尔多瓦工作

标签: javascript html css arrays


【解决方案1】:

您不能在 Cordova 的 html 文件中运行 javascript。您需要在 index.js 文件中添加一个事件监听器:

 document.getElementById('submit').addEventListener('click', validate);

然后在同一个文件中编写您的函数 validate()

【讨论】:

    猜你喜欢
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多