dqhdpppm02183 2013-06-21 09:18
浏览 22
已采纳

如果javascript条件为true则重定向不起作用

I have a form and I validate the fields in javascript functions. After the validation, I want to redirect to another page. I am trying this for the form:

<form action="" method="post" name="form" onsubmit="return validate()">
User Name : <input type="text" name="realname" size="19"><span id="realnameerror" ></span>
<br>
E-Mail    : <input type="text" name="email" size="25"><span id="emailerror" ></span>
<br>
PhoneNo   : <input type="phoneno" name="phoneno" maxlength="10" size="25"><span id="phonenoerror" ></span>
<br>
<input type="submit" value="Submit">
</form>

And this is the code for validation:

<script type="text/javascript">

var hasFocus = false;
function checkName(form)       /* for name validation */   
{...}
function checkEmail(form)          /* for email validation */
{...}
function validPhone(form)              /* for phone validation */
{...}
function validate() 
{
hasFocus = false;
var form = document.forms['form'];
var ary=[checkName,checkEmail,validPhone];
var rtn=true;
var z0=0;
for (var z0=0;z0<ary.length;z0++)
{
if (!ary[z0](form))
{
rtn=false;
}
}
 if (rtn)
   {
      window.location="http://test.dev";
      return rtn;
   }
  else return rtn;
}
</script>

The point is that all the javascript functions are working correctly, I get error messages if there are any, but it just doesn't make my redirect. The weird thing is that if I put the redirect into another script, and don't make the validation, it works. I don't know what am I doing wrong. I have tried to put my redirect into another function and just call it like this:

if (rtn) {  Redirect(); }

but it still doesn't work. Instead of window.location I also tried window.location.href and document.location.href. I really think there something that I'm missing inside the script... If you notice something, please let me figure it out. Thank you!

I HAVE TRIED TO PUT AN ALERT INSIDE MY IF STATEMENT AND IT APPEARS FOR 2 SECONDS AND THEN IT MAKES THE REDIRECT. IF I DON'T PUT THAT ALERT, MY REDIRECT DOESN'T WORK. HERE IS THE CODE:

if (rtn) {
    window.location="http://test.dev";
    alert(rtn);
   }
  • 写回答

6条回答 默认 最新

  • duan19805 2013-06-21 09:28
    关注

    alert something inside this code

    if (rtn)
    {
      alert('you here');
      window.location="http://test.dev";
      return rtn;
    }
    

    if alert come out, you got here. If not, your first condition is wrong. That we split out where to have problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题