函数在Javascript中不带setTimeout()的情况下立即运行
我想用HTML、CSS、Javascript、PHP和MySQL构建一个web。我想我的网站将有一个功能,客户希望从网上下载文件,如果他们不想下载更快,不需要通过缩短链接,他们将输入代码,网站将使用PHP和MySQL检查代码是否在数据库中。如果代码有效,他们将按“下一步”按钮转到下载文件。 这是我的全部代码: HTML+CSS+JS+PHP:函数在Javascript中不带setTimeout()的情况下立即运行,javascript,php,html,css,mysql,Javascript,Php,Html,Css,Mysql,我想用HTML、CSS、Javascript、PHP和MySQL构建一个web。我想我的网站将有一个功能,客户希望从网上下载文件,如果他们不想下载更快,不需要通过缩短链接,他们将输入代码,网站将使用PHP和MySQL检查代码是否在数据库中。如果代码有效,他们将按“下一步”按钮转到下载文件。 这是我的全部代码: HTML+CSS+JS+PHP: <!DOCTYPE html> <html> <head> <title>HTML Document<
<!DOCTYPE html>
<html>
<head>
<title>HTML Document</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script>
function countWord() {
var x = document.getElementById("code").value;
if ((x.length)!=12) {
document.getElementById("ok").disabled = true;
} else if ((x.length)==12) {
document.getElementById("ok").disabled = false;
}
}
</script>
<style>
* {
padding:0;
margin:0;
box-sizing:border-box;
}
body {
background-color:#fff;
}
#code {
width:98%;
margin-left:1%;
}
#ok {
width:50%;
margin-left:25%;
margin-top:10px;
}
#nextbtn {
width:50%;
margin-left:25%;
margin-top:10px;
}
</style>
</head>
<body>
<form method="POST" name="code_form" id="code_form">
<input type="text" class="form-control" name="code" id="code" onkeyup="countWord();">
<input type="submit" name="ok" id="ok" class="btn btn-primary" disabled>
</form>
<button type="button" disabled class="btn btn-primary" id="nextbtn">Next</button>
<?php
$conn = mysqli_connect("","","","");
if (isset($_POST["ok"])) {
$code = $_POST["code"];
$sql = "SELECT * FROM code WHERE code='$code'";
$result = mysqli_query($conn,$sql);
if (mysqli_num_rows($result) == 1) {
echo "
<script>
var x = setTimeout(function(){
document.getElementById('nextbtn').disabled = false;
document.getElementById('nextbtn').onclick = function() {
window.open('https://www.youtube.com');
};
},0);
</script>
";
} else {
echo "<script>alert('Error');</script>";
}
}
?>
</body>
</html>
HTML文档
函数countWord(){
var x=document.getElementById(“代码”).value;
如果((x.长度)!=12){
document.getElementById(“ok”).disabled=true;
}如果((x.长度)==12){
document.getElementById(“ok”).disabled=false;
}
}
* {
填充:0;
保证金:0;
框大小:边框框;
}
身体{
背景色:#fff;
}
#代码{
宽度:98%;
左缘:1%;
}
#嗯{
宽度:50%;
左边缘:25%;
边缘顶部:10px;
}
#下一个{
宽度:50%;
左边缘:25%;
边缘顶部:10px;
}
下一个
仅仅删除setTimeout
是不够的吗
<script>
(function(){
document.getElementById('nextbtn').disabled = false;
document.getElementById('nextbtn').onclick = function() {
window.open('https://www.youtube.com');
})();
</script>
(功能(){
document.getElementById('nextbtn')。disabled=false;
document.getElementById('nextbtn')。onclick=function(){
打开窗户https://www.youtube.com');
})();
此函数将立即调用
(函数(){
document.getElementById('nextbtn')。disabled=false;
document.getElementById('nextbtn')。onclick=function(){
打开窗户https://www.youtube.com');
})()
document.getElementById('nextbtn')。disabled=false;
document.getElementById('nextbtn')。onclick=function(){
打开窗户https://www.youtube.com');
};
//---
HTML文档
函数countWord(){
var x=document.getElementById(“代码”).value;
如果((x.长度)!=12){
document.getElementById(“ok”).disabled=true;
}如果((x.长度)==12){
document.getElementById(“ok”).disabled=false;
}
}
* {
填充:0;
保证金:0;
框大小:边框框;
}
身体{
背景色:#fff;
}
#代码{
宽度:98%;
左缘:1%;
}
#嗯{
宽度:50%;
左边缘:25%;
边缘顶部:10px;
}
#下一个{
宽度:50%;
左边缘:25%;
边缘顶部:10px;
}
下一个
<script>
(function(){
document.getElementById('nextbtn').disabled = false;
document.getElementById('nextbtn').onclick = function() {
window.open('https://www.youtube.com');
})();
</script>
<script>
document.getElementById('nextbtn').disabled = false;
document.getElementById('nextbtn').onclick = function() {
window.open('https://www.youtube.com');
};
</script>
<html>
<head>
<title>HTML Document</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<script>
function countWord() {
var x = document.getElementById("code").value;
if ((x.length) != 12) {
document.getElementById("ok").disabled = true;
} else if ((x.length) == 12) {
document.getElementById("ok").disabled = false;
}
}
</script>
<style>
* {
padding:0;
margin:0;
box-sizing:border-box;
}
body {
background-color:#fff;
}
#code {
width:98%;
margin-left:1%;
}
#ok {
width:50%;
margin-left:25%;
margin-top:10px;
}
#nextbtn {
width:50%;
margin-left:25%;
margin-top:10px;
}
</style>
</head>
<body>
<form method="POST" name="code_form" id="code_form">
<input type="text" class="form-control" name="code" id="code" onkeyup="countWord();">
<input type="submit" name="ok" id="ok" class="btn btn-primary" disabled>
</form>
<button type="button" disabled class="btn btn-primary" id="nextbtn">Next</button>
<?php
//$conn = mysqli_connect("", "", "", "");
if (isset($_POST["ok"])) {
$code = $_POST["code"];
//$sql = "SELECT * FROM code WHERE code='$code'";
//$result = mysqli_query($conn, $sql);
//if (mysqli_num_rows($result) == 1) {
if (1 == 1) {
echo "<script>
document.getElementById('nextbtn').disabled = false;
document.getElementById('nextbtn').onclick = function() {
window.open('https://www.youtube.com');
};
</script>";
} else {
echo "<script>alert('Error');</script>";
}
}
?>
</body>