JavaScript 创建"提交"按钮用于提交表单,并创建另一个按钮用于清除输入
javascriptweb developmentobject oriented programming更新于 2025/10/29 20:28:17
使用 input type=”submit” 提交表单,使用另一个 input type=”button” 单击时清除输入,如以下代码所示 −
示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initialscale=
1.0">
<title>Document</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fontawesome/4.7.0/css/font-awesome.min.css">
</head>
<style>
</style>
<body>
<label>StudentName:
<input type="text" id="txtName" />
</label>
<br>
<button type="submit">Submit</button>
<button type=" button" value="delete"
onclick="deleteSomething()">Delete</button>
<script>
function deleteSomething(secondValue) {
alert("I am going to clear text box data only...");
document.getElementById("txtName").value = "";
}
</script>
</body>
</html>
要运行上述程序,请保存文件名"anyName.html(index.html)",然后右键单击该文件。选择选项"使用 Live Server 打开"在 VS Code 编辑器中。
输出
将产生以下输出 −

现在,我将在文本框中输入一些值 −

现在,我将单击"删除"按钮。点击后,您将收到一条警告消息 −

点击"确定"按钮后,您将获得以下输出 −

相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

