代码不起作用。我无法确定问题

时间:2015-02-28 04:47:54

标签: javascript html web

我正在编写一些代码,由于某些原因我不知道,每当我点击按钮时,它所分配的功能都不会运行。有人知道为什么吗?提前谢谢。

<html>
<head>
</head>
<body>


<script>

function prime(number) {
var text = document.getElementById("p").innerHTML;
var n = 0;

for(var i = 2; i<number; i++){
if(number%i==0){
text = "Your number, "+number+", is divisible by "+i+"! It's composite!";
n = 1;
}
}

if(n==0){
text = "Your number, "+number+", is prime!";
}

}

function funcito(){
console.log("Functions!");
var number = document.getElementById("input");
prime(number);    
}

</script>


<p id="p"></p><br>

<form id="input">
Your Number: <input type="number" name="uInput"><br>
</form> 

<p>Click "Got my number" to find out!.</p>
<button onclick="funcito()" value = "Got my number">Got my number</button>

</body>
</html>

3 个答案:

答案 0 :(得分:1)

试试这个

<button onclick="funcito()" value = "Got my number">Got my number</button>

答案 1 :(得分:0)

因为你写了onclick="funcito"这是错误的写onclick="funcito()"。代替。

答案 2 :(得分:0)

该代码还有其他一些问题,因此您无法获得所需的结果。试试这段代码:

<html>
<head>
</head>
<body>


<script>

function prime(number) {
var text = document.getElementById("p");
var n = 0;

for(var i = 2; i<number; i++){
if(number%i==0){
text.innerHTML = "Your number, "+ number+", is divisible by "+i+"! It's composite!";
n = 1;
}
}

if(n==0){
text.innerHTML = "Your number" + number + " is prime!";
}

}

function funcito(){
console.log("Functions!");
var number = document.getElementById("input").value;
prime(number);    
}

</script>


<p id="p"></p><br>

<form>
Your Number: <input id = "input" type="number" name="uInput"><br>
</form> 

<p>Click "Got my number" to find out!.</p>
<button  onclick="funcito()" value = "Got my number">Got my number</button>

</body>
</html>