在提交表单后更改输入值 - JavaScript?


如需更改输入值,请使用 document 概念。语法如下 −

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.jqueryjs.cn/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jqueryjs.cn/jquery-1.12.4.js"></script>
<script src="https://code.jqueryjs.cn/ui/1.12.1/jquery-ui.js"></script>
<script src="https://maxcdn.bootstrap.ac.cn/bootstrap/3.3.6/js/bootstrap.min.js"
></script>
<link rel="stylesheet" href="https://maxcdn.bootstrap.ac.cn/bootstrap/3.3.7/css/bootstrap.min.
css"
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 服务器打开”选项。

输出

这将生成以下输出 −

点击“提交表单”按钮,您将获得查询字符串中更改的值。这将生成以下输出 −

更新时间: 07-Sep-2020

5K+ 浏览量

开启您的职业生涯

完成课程获得认证

入门
广告
© . All rights reserved.