Javascript 从html代码体调用js方法,并为方法引入变量
我有以下html代码:Javascript 从html代码体调用js方法,并为方法引入变量,javascript,html,Javascript,Html,我有以下html代码: <html> <title> Upload Malware File </title> <body> <header id = "header"> <h1 align="center">Upload Malware File</h1> <p align="center"> Please choose the malware file yo
<html>
<title> Upload Malware File </title>
<body>
<header id = "header">
<h1 align="center">Upload Malware File</h1>
<p align="center"> Please choose the malware file you want to upload.</p>
</header>
<form method="post" enctype="multipart/form-data" action="malware_upload.php">
Select malware file to upload:
<input type="file" name="file"required>
<form method="post" enctype="multipart/form-data">
<div >
<label for="mname"><b>Malware Name</b></label>
<input type="text" placeholder="Malware Name" name="mname" <required> *HERE*
</div>
<div >
<label for="uname"><b>Admin Username</b></label>
<input type="text" placeholder="Enter Username" name="uname" required>
</div>
<div>
<label for="psw"><b>Admin Password</b></label>
<input type="password" placeholder="Enter Password" name="psw" required>
</div>
<input type="submit" value="Upload" name="submit">
</form>
</form>
<form>
<input type="submit" formaction="file_upload.php" value="Go to File Uploads" />
<input type="submit" formaction="CreateAdmin.php" name="SI" value="Sign In" />
</form>
<form method="post" enctype="multipart/form-data">
<input type="submit" name = "logout" value="Logout" />
</form>
如何在html代码中的“HERE”标记处干净地调用checkMalwareName,并在错误为false时返回错误?必须将函数绑定到元素的事件(
oninput
):
改变
<input type="text" placeholder="Malware Name" name="mname" <required>
为什么选择使用oninput而不是onchange?@Riz waan,区别在于oninput
事件在元素的值更改后立即发生,而onchange
事件在元素失去焦点、内容更改后发生。
<input type="text" placeholder="Malware Name" name="mname" <required>
<input type="text" placeholder="Malware Name" name="mname" oninput="checkMalwareName(this.value)" required >