我的目的:如果用户字段和密码字段为空,我想停止表单提交.这是我正在尝试的代码:
<!DOCTYPE html>
<html>
<head>
<script>
function doit() {
var usr = document.getElementById('ur').value;
var psw = document.getElementById('pw').value;
if ((usr.trim() == '') && (psw.trim() == '')) {
alert("cannot Submit form");
return false;
}
}
</script>
</head>
<body>
<form action="post.php" method="post" onsubmit="doit()">
User:
<input type="text" id="ur" name="user">
<br>
<br> Pass:
<input type="password" id="pw" name="pass">
<br>
<br>
<button>Submit</button>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我正在学习JavaScript.如果你纠正代码并稍微解释为什么它不起作用将会有所帮助.
我正在学习Javascript而没有得到我想要的输出.如果你能纠正我的想法,我会很有帮助.
这是我的简单代码:
<script>
window.onload = myFunction ;
function myFunction(){
var i = 0 ;
var counter = 1 ;
while(i < counter){
alert(counter);
counter = counter - 0.1 ;
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
我想要的输出:
1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
Run Code Online (Sandbox Code Playgroud)
但实际输出:
1
0.9
0.8
0.7000000000000001
0.6000000000000001
0.5000000000000001
0.40000000000000013
0.30000000000000016
0.20000000000000015
0.10000000000000014
1.3877787807814457e-16
Run Code Online (Sandbox Code Playgroud) javascript ×2