我在一个带有验证码的表单中使用以下代码。如果验证码正确,则为$success = 1
,否则为$success=2
。若captcha未填写,则为$success=1
。
id=validation
与captcha一起出现在页面的最底部,id=success
与div元素一起出现在页面的最顶部。
问题:
第一次测试:
1) captcha是正确的->点击提交->页面出现在顶部
第二次测试:
1) captcha不正确或未填写->点击提交->页面来到底部
2)验证码是正确的->按回车->页面留在底部(而它应该放到顶部)
var myVariable = <?php echo(json_encode($success)); ?>;
if (myVariable == 2) {
window.location = (""+window.location).replace(/#[A-Za-z0-9_]*$/,'')+"#validation"
};
if (myVariable == 3) {
window.location = (""+window.location).replace(/#[A-Za-z0-9_]*$/,'')+"#validation"
};
if (myVariable == 1) {
window.location = (""+window.location).replace(/#[A-Za-z0-9_]*$/,'')+"#success"
};
试试这个:
<script type='text/javascript'>
var myVariable = <?php echo(json_encode($success)); ?>;
if (myVariable == 2 || myVariable == 3) {
window.location.href = (""+window.location.href).replace(/#[A-Za-z0-9_]*$/,'')+"#validation"
};
if (myVariable == 1) {
window.location.href = (""+window.location.href).replace(/#[A-Za-z0-9_]*$/,'')+"#success"
};
</script>
我改变了窗口。location to window.location。href并在一个if语句中获取相同的结果