简体   繁体   中英

window.location = '' is not working ;

$(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">

In the above code I would like to redirect the page with the help of Javascript, but window.location = 'dws_Support.php' in else part is not working, but when we alert with in the else part it gets displayed but not redirected to 'dws_Support.php' . Please help.

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

you don't need form for redirecting with javascript ,problem come from your attribute action form, so use this code:

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

hope it help you.

Here is your fail: you are listening click , but submit event of the form brakes your logic. This is the fixed one:

$(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';
    }
  });
});

glad if you found the answer, but there is no problem with your coding, you just missed the return false; after redirect..

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

hope this can fix your problem. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM