如何使用jQuery检查文本框中的值是否为素数 [英] How to check if the value in a text box is prime or not with jQuery
本文介绍了如何使用jQuery检查文本框中的值是否为素数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用jQuery确定文本框中的值是否为素数。
这是我到目前为止尝试过的,但它不是工作:
< pre class =snippet-code-js lang-js prettyprint-override>
$(#textbx)。keyup(function(){if($(#textbx)。val() .length> 0){$(#btn)。removeAttr('disabled');}}); $(#textbx)。blur(function(){if($(#textbx)。val()。length == 0){$(#btn)。attr('disabled','disabled ');}}); $(#textbx)。keypress(function(e){if(e.which!= 8&&(e.which< 48 || e.which> 57)){$(#msg ).html(plz press nimber only)。show()。fadeOut(slow); return false;}}); $(#btn)。click(function(){var num = parseInt($(#textbx)。val()); var i; for(var i = 2; i> num / 2; i ++ ){if(i%num == 0){$(#msg)。html(yupp)。show()。fadeOut(slow);} else {$(#msg)。html (huh)。show()。fadeOut(slow);} break;}});
#msg {color:red;}
< script src =https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js>< / script>< input id =textbxtype =text/>< button id =btndisabled =disabled> go< / button> < span id =msg>< / span>
JSFiddle版本: http://jsfiddle.net/akash4pj/72FtQ/
解决方案
这是你的代码略有修改
DEMO http://jsfiddle.net/72FtQ/1/
function isPrime(n){
//如果n小于2或不是整数,那么根据定义,不能是素数。
if(n< 2){return false}
if(n!= Math.round(n)){return false}
//现在检查每个整数从2到n的平方根。如果这些中的任何一个完全划分,则n不能是素数。
for(var i = 2; i< = Math.sqrt(n); i ++){
if(n%i == 0){return false}
}
//如果n%i从未上面== 0,那么n必须是素数
返回true;
}
SOURCE http://studymaths.co.uk/topics/checkIfPrime.php
I am trying to determine whether the value in a text box is prime or not using jQuery.
Here is what I've tried so far, but it's not working:
$("#textbx").keyup(function(){
if ($("#textbx").val().length > 0) {
$("#btn").removeAttr('disabled');
}
});
$("#textbx").blur(function(){
if ($("#textbx").val().length ==0) {
$("#btn").attr('disabled','disabled');
}
});
$("#textbx").keypress(function (e) {
if (e.which!=8 && (e.which < 48 || e.which > 57)) {
$("#msg").html("plz press nimber only").show().fadeOut("slow");
return false;
}
});
$("#btn").click(function(){
var num =parseInt($("#textbx").val());
var i;
for (var i = 2; i >num/2; i++) {
if(i%num==0)
{ $("#msg").html("yupp").show().fadeOut("slow");
}
else
{$("#msg").html("huh").show().fadeOut("slow");
}
break;
}
});
#msg
{
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<input id="textbx" type="text" /><button id="btn" disabled="disabled">go</button>
<span id="msg"></span>
JSFiddle version: http://jsfiddle.net/akash4pj/72FtQ/
解决方案
Here is your code slightly modified
DEMO http://jsfiddle.net/72FtQ/1/
function isPrime(n) {
// If n is less than 2 or not an integer then by definition cannot be prime.
if (n < 2) {return false}
if (n != Math.round(n)) {return false}
// Now check every whole number from 2 to the square root of n. If any of these divides n exactly, n cannot be prime.
for (var i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {return false}
}
// If n%i was never == 0 above, then n must be prime
return true;
}
SOURCE http://studymaths.co.uk/topics/checkIfPrime.php
这篇关于如何使用jQuery检查文本框中的值是否为素数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文