如果字段为空则问题是验证,那么它应该提供警报但是没有响应点击。这个验证jquery错误或函数是不是被正确调用了?
我查了一遍,我尝试了不同的java脚本,它没有用,任何解决方案
<html>
<head>
<title>PhoneGap</title>
<link rel = "stylesheet" href="css/theme.min.css"/>
<link rel="stylesheet" href="css/jquery.mobile-1.2.1.css"/>
<script type="text/javascript" charset="utf-8" src="js/jquery-1.6.1.min.js"></script>
<script type="text/javascript" charset="utf-8" src="cordova-2.7.0.js"></script>
<script type="text/javascript" charset="utf-8" src="js/jquery.mobile-1.2.1.min.js"></script>
<script type="text/javascript" >
<!--wait till cordnova loads-->
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
navigator.splashscreen.show();
navigator.splashscreen.hide();
}
</script>
</head>
<body>
<div data-role="page" id="page1" data-theme="b" >
<div id="header" data-role="header">
<h3>Login Page</h3>
</div>
<div id="wrapper">
<form action="" name="myLogin">
<div data-role="content">
<div class="username" data-role="fieldcontain">
<label for="username"> Username </label>
<input name="username" id="usename" placeholder="Enter Username" value="" type="text">
</div>
<div class="password" data-role="fieldcontain">
<label for="password">Password</label>
<input name="password" id="password" placeholder="Enter Password" value="" type="text">
</div>
<input id="login" value="login" type="button" onclick="validationcheck();">
<input id="register" value="register" type="button" onclick="register();">
</div>
</form>
</div>
</div>
<script type="text/javascript">
function validationcheck(){
if (document.mylogin.username.value == "") {
alert("Please Enter Username.");
document.mylogin.username.focus();
} else if (document.mylogin.password.value == "") {
alert("Please Enter Password.");
document.mylogin.password.focus();
}
else
{
alert("success");
}
}
</script>
</body>
</html>
答案 0 :(得分:3)
首先,修复用户名输入id中的拼写错误:从“username”到“username”。现在你的功能应该如何:
function validationcheck(){
if ($("#username").val() == "") {
alert("Please Enter Username.");
$("#username").focus();
} else if ($("#password").val() == "") {
alert("Please Enter Password.");
$("#password").focus();
}
else
{
alert("success");
}
}
您也可以在不使用JQuery的情况下执行此操作:只需使用
即可document.getElementById('username').value
document.getElementById('username').focus()
答案 1 :(得分:0)
试试这个兄弟
删除您的form
代码和onclick="validationcheck();"
HTML
<div data-role="content">
<div class="username" data-role="fieldcontain">
<label for="username"> Username </label>
<input name="username" id="username" placeholder="Enter Username" value="" type="text">
</div>
<div class="password" data-role="fieldcontain">
<label for="password">Password</label>
<input name="password" id="password" placeholder="Enter Password" value="" type="text">
</div>
<input id="login" value="login" type="button" >
<input id="register" value="register" type="button" onclick="register();">
</div>
使用jquery代替传统的javascript
的javascript
$("#login").click(function() {
if ($("#username").val() == "") {
alert("Please enter username.");
} else if ($("#password").val() == "") {
alert("Please enter password.");
} else {
alert("success");
}
});