【问题标题】:window.location = '' is not working ;window.location = '' 不工作;
【发布时间】:2015-09-28 06:26:53
【问题描述】:
$(document).ready(function() {
  $("#submit").click(function() {
    var value = document.getElementById("txt_sketch_no").value;
    var process_id = document.getElementById("process_id").value;
    var length = value.length;
    if (length <= 0) {
      alert("Please Enter the sketch card");
      window.location = 'sketch_screen.php';
    } else {
      window.location = 'dws_Support.php';
    }
  });
});
<form action="" method="post" autocomplete="off">
  <div class="container">
    <div class="alert alert-success" id="alert_box">
      <input type="hidden" name="process_id" id="process_id" value="<?php echo $process_id; ?>">
      <label>Enter the Sketch Card No :</label>
      <input type=text id="txt_sketch_no" name="txt_sketch_no">
      <input type=submit id="submit">

在上面的代码中,我想在 Javascript 的帮助下重定向页面,但是 window.location = 'dws_Support.php' 在 else 部分不起作用,但是当我们在 else 部分发出警报时,它会显示但不会重定向到 'dws_Support.php' .请帮忙。

【问题讨论】:

  • 使用 window.location.href 或 window.location.assign
  • 我的猜测是您在使用 jquery 验证之前提交了表单。尝试先验证它。 stackoverflow.com/a/15072147/797495
  • 您确定if 部分中的重定向有效吗?
  • 这行得通吗? if (length &lt;= 0) { alert("Please Enter the sketch card"); window.location = 'sketch_screen.php'; return; } else { window.location = 'dws_Support.php'; }
  • @MrLister,我使用了 sn-p 函数中的 tidy 按钮。没有注意到它创建了一个错误。显然我不是故意的……

标签: javascript php jquery html


【解决方案1】:

尝试使用 window.location.href='dws_Support.php'

【讨论】:

    【解决方案2】:

    您不需要表单来使用 javascript 进行重定向,问题来自您的属性操作表单,因此请使用以下代码:

    <script src="js/jquery.js"></script>
    <script>
        $(document).ready(function() {
            $("#submit").click(function() {
                var value = document.getElementById("txt_sketch_no").value;
                var process_id = document.getElementById("process_id").value;
                var length = value.length;
                if (length <= 0) {
                    alert("Please Enter the sketch card");
                    window.location = 'sketch_screen.php';
                } else {
                    window.location = 'dws_Support.php';
                }
            });
        }); 
    </script>
    </head>
    <body style="background-color: #73C5E1" >
            <div class="container">
                <div class="alert alert-success" id="alert_box">
                    <input type="hidden" name="process_id" id="process_id" value="12">
                    <label> Enter the Sketch Card No : </label>
                    <input type="text" id="txt_sketch_no" name="txt_sketch_no">
                    <input type = "submit"  id="submit">
                </div>
            </div>
    </body>
    

    希望对你有帮助。

    【讨论】:

      【解决方案3】:

      这是您的失败:您正在收听click,但submit 表单事件破坏了您的逻辑。这是固定的:

      $(document).ready(function() {
        $("form").on('submit', function(e) {
          e.preventDefault();
      
          var value = document.getElementById("txt_sketch_no").value;
          var process_id = document.getElementById("process_id").value;
          var length = value.length;
          if (length <= 0) {
            alert("Please Enter the sketch card");
            window.location = 'sketch_screen.php';
          } else {
            window.location = 'dws_Support.php';
          }
        });
      });
      

      【讨论】:

        【解决方案4】:

        很高兴您找到了答案,但您的编码没有问题,您只是错过了 return false;重定向后..

        <script src="jquery-1.4.js"></script>
        <script type="text/javascript">
        <!--
            $(document).ready(function() {
              $("#submit").click(function() {
                var value = document.getElementById("txt_sketch_no").value;
                var process_id = document.getElementById("process_id").value;
                var length = value.length;
                if (length <= 0) {
                  alert("Please Enter the sketch card");
                  window.location = '/sketch_screen.php';
                  return false;
                } else {
                  window.location = 'dws_Support.php';
                  return false;
                }
          });
        });
        //-->
        </script>
        

        希望这可以解决您的问题。 :)

        【讨论】:

          猜你喜欢
          • 2017-01-09
          • 1970-01-01
          • 2023-02-05
          • 2011-08-30
          • 2020-06-30
          • 1970-01-01
          • 2013-11-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多