如何在javascript中通过输入接收数字并仅返回偶数&;jquery?
如何在javascript和jquery中通过输入接收数字并只返回偶数如何在javascript中通过输入接收数字并仅返回偶数&;jquery?,javascript,jquery,arrays,Javascript,Jquery,Arrays,如何在javascript和jquery中通过输入接收数字并只返回偶数 $(document).ready(function() { $("#submit").click(function() { var nums = $("#nums").val(); var numsf = nums.split(','); var res = numsf.join(", "); var even = $("p").ap
$(document).ready(function() {
$("#submit").click(function() {
var nums = $("#nums").val();
var numsf = nums.split(',');
var res = numsf.join(", ");
var even =
$("p").append(even + "<br>");
});
});
$(文档).ready(函数(){
$(“#提交”)。单击(函数(){
var nums=$(“#nums”).val();
var numsf=nums.split(',');
var res=numsf.join(“,”);
var偶数=
$(“p”)。追加(偶数+”
”;
});
});
使用模数2检测它是否为偶数
$(document).ready(function () {
$("#submit").click(function () {
var nums = parseInt($("#nums").val());
var isEven = nums % 2 == 0;
if(isEven){
$("p").append(nums);
}
});
});
更新
$(document).ready(function() {
$("#submit").click(function() {
var nums = $("#nums").val();
var numsf = nums.split(',');
var evens = [];
for (var i = 0; i < numsf.length; i++) {
if (parseInt(numsf[i]) % 2 == 0) {
evens.push(numsf[i]);
}
}
var res = evens.join(", ");
$("p").append(res + "<br>");
});
});
$(文档).ready(函数(){
$(“#提交”)。单击(函数(){
var nums=$(“#nums”).val();
var numsf=nums.split(',');
var evens=[];
对于(变量i=0;i”);
});
});
这可以解决您的问题。我已经提供了演示链接
$(“文档”).ready(函数(){
$(“#提交”)。单击(函数(){
var nums=$(“#nums”).val();
var numsf=nums.split(',');//逗号分隔值数组
var evens=[];
//遍历数组。请参阅jQuery.each()的文档
$.each(numsf,函数(i,_this){
如果(!isNaN(_this)&&parseInt(_this)%2==0)
平推(这个);
});
var res=evens.toString();
$(“p”)。追加(res+“
”);
});
});
试试这个
$(文档).ready(函数(){
var numsf=[1,2,3,4,5,6,7,8,9];
$。每个(numsf,函数(a,b){
如果(b%2==0){
警报(b);
返回b;
}
})
})
我个人建议如下——假设带有id=“nums”
的元素是以逗号分隔的数字字符串:
// binding an anonymous function tot he click event
// of the element with 'id="submit"', passing the
// event ('e') into the function:
$("#submit").click(function (e) {
// preventing any default behaviour of the
// element's click event:
e.preventDefault();
// updating the text of the <p> element(s)
// (it would be better, in practice, to use
// a more specific selector):
$('p').text(function () {
// splitting the value of the '#nums' element on the
// comma (',') character to form an array; using
// Array.prototype.filter() to filter that array:
return $('#nums').val().split(',').filter(function (n){
// the first argument to the anonymous function
// (here 'n') is the array element of the array
// over which we're iterating.
// we trim the number-string (removing leading
// and trailing spaces), and then use parseInt()
// to convert that to a number in base 10 (the
// second, the radix, argument to parseInt());
// if the remainder of the division of that number
// by two is 0 then the number is even and
// the assessment evaluates to true, so we
// keep that number (discarding those numbers
// for which the assessment evaluates to false):
return parseInt(n.trim(), 10) % 2 === 0;
// joining the filtered-array back together:
}).join(', ');
});
});
p::before{
内容:“偶数:”;
显示:块;
颜色:#999;
}
p:空::之前{
内容:'';
}
输入逗号分隔的数字:
提交
@VateX,怎么办?是否发生异常?你是说别的吗?“你能用小提琴把它复制出来吗?”@BhojendraNepal,对不起,他的意思是Vatex@AmmarCSE:我们的解决方案有一个小问题,但我只是在测试您的代码时才注意到。如果我们在文本框中提供类似“1a”的内容,它也会添加到结果中。现在我意识到我犯了一个错误。我真的很抱歉,我改变了我的投票。我添加了!伊斯南(_this)
到if
状态。@RejithRKrishnan,即使你投了反对票,也没有什么不快的感觉。我只想解释一下为什么会出现否决票,这样我就可以更正我的答案了。谢谢你的家庭作业问题!看看我的答案。如果你能解释一下这是如何回答问题的,代码是如何工作的,以及它是做什么的,那就更有用了。
// binding an anonymous function tot he click event
// of the element with 'id="submit"', passing the
// event ('e') into the function:
$("#submit").click(function (e) {
// preventing any default behaviour of the
// element's click event:
e.preventDefault();
// updating the text of the <p> element(s)
// (it would be better, in practice, to use
// a more specific selector):
$('p').text(function () {
// splitting the value of the '#nums' element on the
// comma (',') character to form an array; using
// Array.prototype.filter() to filter that array:
return $('#nums').val().split(',').filter(function (n){
// the first argument to the anonymous function
// (here 'n') is the array element of the array
// over which we're iterating.
// we trim the number-string (removing leading
// and trailing spaces), and then use parseInt()
// to convert that to a number in base 10 (the
// second, the radix, argument to parseInt());
// if the remainder of the division of that number
// by two is 0 then the number is even and
// the assessment evaluates to true, so we
// keep that number (discarding those numbers
// for which the assessment evaluates to false):
return parseInt(n.trim(), 10) % 2 === 0;
// joining the filtered-array back together:
}).join(', ');
});
});