javascript - 在提交之前使用 JavaScript 更改 HTML 表单值
问题描述
使用外部 JavaScript 文件,我将下面这个按钮的值更改为“5”。
这行得通,我使用 console.log 检查该值是否确实更改为“5”。
<form action="getdata.php" method="POST">
<button type="submit" class="firstHeavyGame button" name='storrage' value=''>PRESS THE BUTTON</button>
</form>
然后,当单击此按钮时,它会将值发送到处理数据的 php 站点。
但是,我认为然后单击此按钮,该值将重置为“”“”(空白)。基本 HTML(我是新手)。
所以 php 脚本没有接收到任何数据。
我通过手动输入值来检查其他一切是否正常:
<form action="getdata.php" method="POST">
<button type="submit" class="firstHeavyGame button" name='storrage' value='5'>PRESS THE BUTTON</button>
</form>
这可以正常工作(获取数字 5 并将其传递给处理它的 php 文件)。
现在我知道我犯了一个新手错误,但在进行研究时我似乎无法为我的例子找到答案。
希望你们能帮帮我!
解决方案
你可以试试:
<form action="getdata.php" method="POST" onSubmit="setValue(this)">
<button type="submit" class="firstHeavyGame button" name='storrage' value=''>PRESS THE BUTTON</button>
</form>
<script>
function setValue(v) {
v.storrage.value="5";
return true;
}
</script>
推荐阅读
- scikit-learn - 如何使用加权 log_loss 作为 linear_model.SGDClassifier 的 SCORING 函数?
- java - 插入 sql db 失败,异常:java com.microsoft.sqlserver.jdbc.SQLServerException:必须声明标量变量“@P7@P8”
- angular - ng-material-multilevel-menu 在 Angular 7 中不起作用
- rust - 为什么胖指针有时会向外渗透?
- python - 将行中的行项目转换为数据框中的列
- excel - 需要在excel中的某个给定间隔之后从给定日期生成下一个日期
- r - 将每个单元格中的值除以 R 中的组平均值
- spring-boot - Spring Boot 2.1:类路径资源 [keystore.p12] 无法解析为 URL,因为它不存在
- python - python打印类的实例
- webpage - Google 云端硬盘图片查看限制