通过外部 JavaScript 文件,我将下面此按钮的值更改为“5”。
这有效,我使用 console.log 来检查该值是否确实更改为“5”。
<form action="getdata.php" method="POST">
<button type="submit" class="firstHeavyGame button" name='storage' value=''>PRESS THE BUTTON</button>
</form>
然后,当单击此按钮时,它会将值发送到处理数据的 php 站点。
但是,我认为单击此按钮后,该值将重置为“”“”(空白)。基本 HTML(我是新手)。
所以 php 脚本没有接收任何数据。
我通过手动输入值来检查其他一切是否正常:
<form action="getdata.php" method="POST">
<button type="submit" class="firstHeavyGame button" name='storage' 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='storage' value=''>PRESS THE BUTTON</button>
</form>
<script>
function setValue(v) {
v.storage.value="5";
return true;
}
</script>
关于javascript - 在提交之前使用 JavaScript 更改 HTML 表单值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59682524/