简体   繁体   English

JavaScript id输出仅显示一秒钟然后消失

[英]JavaScript id output only displays for a second then dissapears

I am making a simple JavaScript function where you enter two numbers in a form and then the function determines if the sum of the two numbers is a prime. 我正在制作一个简单的JavaScript函数,您以一种形式输入两个数字,然后该函数确定两个数字的和是否为质数。 It all works but when the function displays a message with the document.getElementById("demo").innerHTML and 一切正常,但是当函数显示带有document.getElementById(“ demo”)。innerHTML和

it is only on the web page for one second then disappears. 它仅在网页上停留一秒钟,然后消失。 I want to make it so the message stays until you click on the button again. 我要这样做,以便消息一直保留到您再次单击该按钮为止。 I have searched this site and other site but have not been able to find anything. 我已经搜索了该站点和其他站点,但是找不到任何东西。

Here is the code: 这是代码:

 <!DOCTYPE html> <html> <head> <link href="/prime.css" rel="stylesheet" type="text/css" /> <script> function prime() { // get the values from the form var x= document.forms["frm1"]["x"].value; var y= document.forms["frm1"]["y"].value; //test the values of the form if (x == "null"|| x=="" || isNaN(x)|| y=="null" || y==""|| isNaN(y)){ alert("You must enter two numbers."); exit; } //change variables to number and add them together var number = (parseInt(x)+ parseInt(y)); var a=""; var prime = true; //check to make sure number is not less than one if (number <= 1){ alert("Sorry " +number+ " is not a prime."); prime = false; exit; } //check if number is a prime for (var dividby = 2; dividby <= number / 2; dividby++){ if(number % dividby == 0) prime = false; break; } //send congratulations if(prime==true){ a="congratulations " + number + " is a prime!"; } //send condolences else{ a="Sorry "+number+ " is not a prime."; } document.getElementById("demo").innerHTML=a; } </script> </head> <body id="elem"> <div class="div"></div> <div> <br> <h1>PRIME NUMBERS</h1> <p>Please enter two numbers to be added and see if the result is a prime.</p><br> <form name="frm1" > <input type="text" name="x"> + <input type="text" name="y"> <button onclick="prime()">click me</button> </form> <br> <p id="demo"></p> </div> </body> </html> 

The problem is that your "click me" button is actually submitting the form, causing the page to reload (since your <form> doesn't have an action attribute, it submits to the same page). 问题是您的“单击我”按钮实际上是在提交表单,从而导致页面重新加载(由于您的<form>没有action属性,因此它将提交到同一页面)。 The <button> tag defaults to type="submit" . <button>标记默认为type="submit" To make it NOT a submit button, you need to change it like this: 要使其不是“提交”按钮,您需要像这样更改它:

<button onclick="prime()" type="button">click me</button>

I just tried the code, and what happens is simple: your form is posted when you click the button. 我只是尝试了代码,然后发生的事情很简单:单击按钮后,表单就会发布。

In the button onClick, write onClick=" return prime();" 在按钮onClick中,编写onClick =“ return prime();” and add a 并添加一个

return false;

in your javascript function. 在您的javascript函数中。

In your form tag, just add onsubmit="return false;" 在表单标记中,只需添加onsubmit =“ return false;”。 like this- 像这样-

<form name="frm1" onsubmit="return false;" >

This will prevent your form from being submitted. 这将阻止您提交表单。

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

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