简体   繁体   English

按钮不使用onClick重定向-Javascript HTML

[英]Button don't redirect using onClick - Javascript HTML

i'm working in a simple web, and i use a botton for enter to the principal page, but nothing happens, only work if i don't use the function, see: 我正在一个简单的Web中工作,我使用botton进入主体页面,但是什么也没有发生,只有在我不使用该功能的情况下才有效,请参见:

With this don´t work: 与此不起作用:

The button: 按钮:

<center><input type="button" name="Entrar" onClick="iniciar()" value=" Ingresar " >

The function "iniciar()": 函数“ iniciar()”:

    <script type="text/javascript" language="javascript">

function iniciar(){
var value
var clase,nombre,validate="ATS1"
    nombre=window.prompt("Digite su primer nombre");
    clase=prompt("Digite su codigo de seccion de clase (letras en mayuscula)");

    if (clase==validate){
        value = 3;
    }
    else{
        value = 0;
    }

if (value == 3){
    window.alert("Ingreso exitoso, presione Aceptar.")
    var pag="formulario.html"   

    alert("Aceptado \nPulse Aceptar y sera redireccionado en 2 segundos.", setTimeout("location.href=pag",2000))    
     //Dos segundos para redireccionar
}
else{
    alert("Lo sentimos mucho, usted no tiene acceso a la pagina. Usted pertenece a otro curso, adios.")
    close()}
}

</script>

But if i don't use the button, i use the script of java directly (no function) in the page this is working (redirect) 但是,如果我不使用按钮,则可以在页面中直接使用Java脚本(无功能)(重定向)

<script type="text/javascript" language="javascript">

var value
var clase,nombre,validate="ATS1"
    nombre=window.prompt("Digite su primer nombre");
    clase=prompt("Digite su codigo de seccion de clase (letras en mayuscula)");

    if (clase==validate){
        value = 3;
    }
    else{
        value = 0;
    }

if (value == 3){
    window.alert("Ingreso exitoso, presione Aceptar.")
    var pag="formulario.html"   

    alert("Aceptado \nPulse Aceptar y sera redireccionado en 2 segundos.", setTimeout("location.href=pag",2000))    
     //Dos segundos para redireccionar
}
else{
    alert("Lo sentimos mucho, usted no tiene acceso a la pagina. Usted pertenece a otro curso, adios.")
    close()}


</script>

I hope that they can understand me :/ Thanks 我希望他们能理解我:/谢谢

================================================================= ================================================== ===============

See all the code (i use the syntax that @Moogs send me it's work :D): 查看所有代码(我使用@Moogs发送给我的语法:D):

<!doctype html>
<html>
<head>

<title>Formulario</title>
<script type="text/javascript" language="javascript">
function cerrar(){
    close()
}

function iniciar(){
var value
var clase,nombre,validate="ATS1"
    nombre=window.prompt("Digite su primer nombre");
    clase=prompt("Digite su codigo de seccion de clase (letras en mayuscula)");

    if (clase==validate){
        value = 3;
    }
    else{
        value = 0;
    }

if (value == 3){
    window.alert("Ingreso exitoso, presione Aceptar.")
    var pag="formulario.html"   

    alert("Aceptado \nPulse Aceptar y sera redireccionado en 2 segundos.");
    setTimeout(function() {
             window.location.href = pag;
        }, 2000)    
     //Dos segundos para redireccionar
}
else{
    alert("Lo sentimos mucho, usted no tiene acceso a la pagina. Usted pertenece a otro curso, adios.")
    close()}
}
document.querySelector('[type="button"]').onclick = iniciar;
</script>
</head>

<body>

<center><font face="Lucida handwriting"><h1><b><i>Formulario</i></b></h1></font></center><hr>
<br>
<center><input type="button" name="Entrar" onClick="iniciar()" value=" Ingresar ">
<input type="button" name="Salir" onClick="cerrar()" value=" Salir ">
</center>
</body>
</html>

Use a function for the setTimeout callback instead of an eval string and place the timeout after the alert instead of as an alert argument. 将函数用于setTimeout回调而不是eval字符串,并将超时放置在警报之后,而不是作为警报参数。

 function iniciar() { var value = 0; var clase; var nombre; var validate = "ATS1"; nombre = prompt("Digite su primer nombre"); clase = prompt("Digite su codigo de seccion de clase (letras en mayuscula)"); if (clase === validate) { value = 3; } if (value == 3) { alert("Ingreso exitoso, presione Aceptar."); var pag = "formulario.html" alert("Aceptado \\nPulse Aceptar y sera redireccionado en 2 segundos."); setTimeout(function() { alert('redirect'); window.location.href = pag; }, 2000) } else { alert("Lo sentimos mucho, usted no tiene acceso a la pagina. Usted pertenece a otro curso, adios."); close(); } } document.querySelector('[type="button"]').onclick = iniciar; 
 <input type="button" name="Entrar" value=" Ingresar " > 

Here i use your code it's working fine: 在这里我用你的代码工作正常:

Check it: 核实:

<html>
<head>
</head>
<body>
<style>
</style>
<script type="text/javascript" language="javascript">

function iniciar(){
alert("hello");
var value
var clase,nombre,validate="ATS1"
    nombre=window.prompt("Digite su primer nombre");
    clase=prompt("Digite su codigo de seccion de clase (letras en mayuscula)");

    if (clase==validate){
        value = 3;
    }
    else{
        value = 0;
    }

if (value == 3){
    window.alert("Ingreso exitoso, presione Aceptar.")
    var pag="formulario.html"   

    alert("Aceptado \nPulse Aceptar y sera redireccionado en 2 segundos.", setTimeout("location.href=pag",2000))    
     //Dos segundos para redireccionar
}
else{
    alert("Lo sentimos mucho, usted no tiene acceso a la pagina. Usted pertenece a otro curso, adios.")
    close()}
}

</script>
 <center><input type="button" name="Entrar" onClick="iniciar()" value=" Ingresar " >
</body>
</html>

Check Fiddle here. 在这里检查小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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