在 JavaScript 中更改表单提交时输入的值?
javascriptweb developmentobject oriented programming更新于 2025/11/10 23:08:17
要更改输入的值,请使用文档概念。语法如下 −
document.yourFormName.yourTextName.value=anyValue;
假设以下是我们的输入类型,在单击"提交表单"时调用 submitForm() −
<form name="studentForm" onsubmit="submitForm();"> <input type="hidden" name="txtInput" value="10000" /> <input type="submit" value="Submit Form"/> </form>
submitForm() 函数改变输入的值 −
function submitForm(){
document.studentForm.txtInput.value='STUDENT-100';
}
示例
<!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>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"
></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.
css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous">
</head>
<body>
<form name="studentForm" onsubmit="submitForm();">
<input type="hidden" name="txtInput" value="10000" />
<input type="submit" value="Submit Form"/>
</form>
<script>
function submitForm(){
document.studentForm.txtInput.value='STUDENT-100';
}
</script>
</body>
</html>
要运行上述程序,请将文件名保存为"anyName.html(index.html)",然后右键单击该文件。在 VS Code 编辑器中选择选项"使用 Live Server 打开"。
输出
将产生以下输出 −

单击按钮"提交表单"时,您将在查询字符串中获得更改值。这将产生以下输出 −

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

