我使用 javascript 保护我的 html 表单,因为我的用户可以更改输入值,并且我想检查输入值是否等于随机生成的数字。我正在使用 if/else 语句,但 if else 语句不起作用。
var pokeid = '+res.id+'; //generates random id
var html = '';
html += '<form id="myForm" action="myurl.net" method="post">';
html += ' <input type="hidden" id="goid" name="goid" value="'+res.id+'" >';
html += ' <input type="hidden" name="level" value="'+res.level+'" />';
html += ' <input value="Battle!" class="button" onclick="myFunction()" />';
</script>
<script type= "text/javascript">
function myFunction()
{
if (pokeid != document.getElementById("goid").value; ) {
window.location = '("http://www.myurl.net/index.php")';
}else{
document.getElementById("myForm").submit();
}
}
</script>
最佳答案
JavaScript 中没有安全性。所有验证必须在服务器上完成。没有什么可以信任的。
除此之外,if
语句中还有一个杂散分号,并且您的 window.location
赋值应该只是一个 URL。不是那些奇怪的括号、引号……之类的东西。
关于html 表单的 JavaScript 保护不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23642191/