动态改变表单的目标

时间:2012-05-05 18:59:10

标签: php javascript jquery iframe target

我正在尝试在iframe中显示我的登录信息,该iframe在启动页面的中间加载灰色背景,但是我在处理iframe的目标时遇到问题,事情应该如下工作: 1-如果用户成功登录主页应该在父窗口中加载 2-如果错误,则应在iframe中显示错误消息。

我的代码:

<div id="cover5" onclick="javascript:cover5();">
        <iframe name="warning" src="SignIn.php" id="warning" onclick="javascript:cover5();">
        </iframe>
    </div>

SignIn.php

<html>
    <head>
        <script type='text/javascript'>
  function valid(){
   if(document.getElementById('username').value.toString().length>=1 || document.getElementById('password').value.toString().length>=1){
    return true;
   }
   else{
    alert("The Combination of username and password are Incorrect !");
    return false;
   }
  }
 </script>
 <script type='text/javascript'>
 function ChangeTarget() {
     document.getElementById("login").prop("target", "_parent")
 }
 </script>
<script type='text/javascript'>
function ChangeText(text) {
                document.getElementById("label1").innerHTML= text;

            }
        </script>
        <title></title>
    </head>
    <body>
<form name="login" id='login' target='_self' method='post' accept-charset='UTF-8'>
<fieldset>
<legend>SignIn</legend>
<input type='hidden' name='submitted' id='submitted' value='1'/>
<label for='username' >Email:</label>
<input type='text' name='email' id='username'  maxlength="50" /> <br />
<label for='password' >Password:</label>
<input type='password' name='password' id='password' maxlength="50" /> <br />
 <br /> <label id="label1" style="color: red"></label> <br />
<input type='submit' name='Submit' value='Login'/> <br />
<a href='resetPassword1.php'> Forgot/Reset Password? Click Here</a>
</fieldset>
</form>

但是,目标不会被调用inisde php代码的方法ChangeTarget()更改,所以问题现在是iframe中的所有内容或父窗口内的所有内容。有谁知道如何解决这个问题?

服务器端脚本:

mysql_select_db("mydb",$check);
$email = $_POST['email'];  //username that will be submit from a form
$password = $_POST['password'];  //password that will be submit from a form

    // if(mysql_num_rows($temp_privileges_id)== 1) //if the id found that means that the user is already assigned to a conference
   //  {
          echo '<script type="text/javascript">',
             'ChangeTarget();',
             '</script>';
          header('Location: main.php'); // transefer to home page with variable username
     }

2 个答案:

答案 0 :(得分:1)

没有方法prop。直接设置target属性的值

document.getElementById("login").target = "_parent";

或使用setAttribute

document.getElementById("login").setAttribute("target", "_parent");

答案 1 :(得分:0)

.prop()是一个jQuery函数。如果您打算在jQuery中执行此操作,请执行以下操作:

$("#login").prop("target", "_parent");

如果您不打算在jQuery中执行此操作,请在直接JavaScript中执行此操作:

document.getElementById("login").target = "_parent";