donglvlao8367 2016-10-20 13:18
浏览 72

window.location第二次没有工作

I am using the following code in a form with a captcha. If the captcha is correct, then $success = 1, else $success=2. If captcha is not filled, then $success=1.

id=validation is mentioned with the captcha which is at the very bottom of the page and id=success is mentioned with a div element which is at the very top of the page.

Problem :

First test :
1) captcha is correct -> hit submit -> page comes to the top

Second test :
1) captcha is not correct or not filled -> hit submit -> page comes to the bottom
2) captcha is correct -> hit enter -> page stay in bottom (whereas it should go to the top)

Java Script :

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"
};
  • 写回答

1条回答 默认 最新

  • dtd14883 2016-10-20 13:22
    关注

    Try this:

    <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>
    

    I changed window.location to window.location.href and took the same result in one if statement.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度