когда триггер войти, выполнить быстро и вернуться?

1

когда я нажимаю кнопку все хорошо, но когда нажмите enter, он вызывает функцию и быстро возвращается в моем браузере, в snippest it desapiar. Я попробовал несколько способов запуска кнопки при нажатии ввода, но я получаю такую же проблему

function calculate() {
          
         
      var response = document.getElementById("text").value;
    
    if (response ==0 || response =="" || response >100 || response < 0) {
        alert('you should write a 0<number<100');
       
        
    } else{
    
     var start = new Date();
     var date = start.getFullYear();
        

    
   var result = date - response;
   
    x.innerHTML = 'Your Birth is '+ result;
    document.getElementById('ma').style.color = 'lightpink';   
    document.getElementById('ma').style.fontFamily = 'sans-serif';
    document.getElementById('ma').style.fontSize = "45px";
      
 
        
    } ;
    


  
  

};

var x= document.getElementById('ma');
document.getElementById("button").onclick = function(){calculate();};


      
   
body {
    outline: none;
}
h1 {
     font-family: fresca;
    outline: none;
    font-size: 35px;
    color:cornflowerblue;
    
}
form {
    margin-top: 20em;
   outline: none;
}

#text{
    width: 35em;
    height: 3em;
    padding-left: 20px;
    border-radius: 5px;
    outline: none;
    
}
#text:focus {
    background: lightblue;
}
#button{
    width: 80;
    height: 3em;
    text-align: center;
    border-radius: 10px;
    border-style: none;
    background: lightblue;
outline: none;
}

#button:hover {
    background: lightgreen;
    
}
<html>

<head>
<link rel="stylesheet" type="text/css" href="css.css">
<link href="https://fonts.googleapis.com/css?family=Fresca" rel="stylesheet">
</head>




<body>
    <center>
    
    <form>
        <input type="text" placeholder="Your Age" id="text" onkeypress="Javascript: if (event.keyCode==13) calculate();" >
        <input type="button" id="button" value="Calculate">
    </form>
        <h1 id="ma" >Manaf</h1>
     
   </center> 
   
    
<script src="script.js";></script>
    
<script
  src="https://code.jquery.com/jquery-3.2.1.min.js"
  integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
  crossorigin="anonymous"></script>
</body>
</html>
Теги:
performance
return
enter
execute

1 ответ

-2
<h1 id="ma" >Manaf</h1>

привет, поскольку вы обертываете форму, поэтому ее триггер на входе

Ещё вопросы

Сообщество Overcoder
Наверх
Меню