我正在为我的网站后端发送/重新发送电子邮件按钮,目前正在尝试找出如何添加2个已经制作了变量的整数。
只要整数不为0,脚本就能正常工作,有人能给我一些指示吗?我需要它来显示总数,无论其中一个变量是否为0,提前谢谢
function sendResend() {
var selected = Array();
var selectedSend = $(".no:checked").length;
var selectedResend = $(".yes:checked").length;
var totalSendResend = parseInt(selectedSend) + parseInt(selectedSend);
$('input:checked').each(function () {
selected.push($(this).attr('name'));
});
var answer = confirm('You have selected ' + totalSendResend + ' emails. ' + selectedSend + ' New emails will be sent & ' + selectedResend + ' will be resent. Are you sure you want to continue?');
if (answer) {
alert(selected);
}
}
答案 0 :(得分:2)
您的代码中有拼写错误:
var totalSendResend = parseInt(selectedSend) + parseInt(selectedSend);
将相同的值加在一起;你想要这个:
var totalSendResend = selectedSend + selectedResend;
您根本不需要parseInt()
演员表,但如果您这样做,则应始终将基数指定为第二个参数,例如: parseInt('0123', 10)
答案 1 :(得分:-1)
当你调用ParseInt(myvalue)
而myvalue
以0开头时,它会返回一个int,考虑到的值以八进制表示。
要避免此行为,请始终使用parseInt(value, 10);
编辑:有关parseInt如何工作的更多信息没有指定基数:
查看更多here