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 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)