简体   繁体   English

在javascript中求和两个值

[英]Sum two values in javascript

this I think will be stupid question, but here it goes. 我认为这将是一个愚蠢的问题,但在这里它。

I need to dynamically add a div and give it ID + 1 depending on last added div's ID of previously added element. 我需要动态添加一个div并给它ID + 1,具体取决于最后添加的div的先前添加元素的ID。

the div is div是

<div id="file-uploader-0" class="file-uploader"></div>

So I roughly came out with that code: 所以我粗略地提出了这个代码:

$('#addOneMoreFileOrGroup').on('click', function(){

    var latestFileUploaderId = $('.well-large .file-uploader').last().attr('id').split('-')[2] + 1;

    $('.well-large .control-group:last').after('<div class="control-group deal-type-online">  \
            <label class="control-label">File / File Group Title:</label> \
            <div class="controls"> \
                <input class="span4 file-title" type="text"> \
                <span class="question label label-warning" data-title="File / Group Name:" data-content="Name for your file or group of related files (for example your brand logotype in different file formats)." data-original-title="">?</span> \
                <div id="file-uploader-' + latestFileUploaderId + '" class="file-uploader"></div> \
            </div> \
        </div>');

    });

But the problem is that in the end of all that I receive "file-uploader-01", "file-uploader-011", "file-uploader-0111". 但问题是,最后我收到“file-uploader-01”,“file-uploader-011”,“file-uploader-0111”。

I tried to use "latestFileUploaderId++" which giving me right result once as "1" and after going as "11", "111", "1111". 我尝试使用“latestFileUploaderId ++”,它给出了正确的结果一次为“1”,然后是“11”,“111”,“1111”。

Thank you for the tips and help! 感谢您的提示和帮助!

Change this: 改变这个:

var latestFileUploaderId = $('.well-large .file-uploader').last().attr('id').split('-')[2] + 1;

to this: 对此:

var latestFileUploaderId = parseInt($('.well-large .file-uploader').last().attr('id').split('-')[2],10) + 1;

You're adding a string to a number, which causes the + operator to do string concatenation. 您正在向一个数字添加一个字符串,这会导致+运算符进行字符串连接。

Convert the string to a number first. 首先将字符串转换为数字。

var latestFileUploaderId = parseFloat($('.well-large .file-uploader').last().attr('id').split('-')[2]) + 1;

Convert the string from the id to integer to increment it correctly: 将字符串中的字符串转换为整数以正确递增:

var lastID = parseInt($('.well-large .file-uploader').last().attr('id').split('-')[2]);
var latestFileUploaderId = lastID + 1;
var latestFileUploaderId = parseInt($('.well-large .file-uploader').last().attr('id').split('-')[2],10) + 1;

你正在共同一个字符串和一个数字... parseInt会将字符串转换为它的数值,然后添加将起作用。

你应该使用parseInt

var latestFileUploaderId = parseInt($('.well-large .file-uploader').last().attr('id').split('-')[2]) + 1;

您可以使用parseInt()将id转换为数字。

var latestFileUploaderId = parseInt($('.well-large .file-uploader').last().attr('id').split('-')[2]) + 1;
$('.well-large .file-uploader').last().attr('id').split('-')[2]

is a string. 是一个字符串。 You need to parse it to a int(float). 您需要将其解析为int(float)。 try using parseInt, parseFloat 尝试使用parseInt,parseFloat

You need to cast the value to a Number. 您需要将值转换为数字。 Like this: 像这样:

var latestFileUploaderId = Number($('.well-large .file-uploader').last().attr('id').split('-')[2]) + 1;

As others have said, you need to change the variable to a Number first, but I wouldn't use parseInt() or similar. 正如其他人所说,你需要先将变量更改为Number,但我不会使用parseInt()或类似的。 Instead, multiplying by 1 is much faster, so something like this is what I'd recommend: 相反,乘以1要快得多,所以我推荐这样的东西:

var last = $('.well-large .file-uploader').last().attr('id').split('-')[2];
var latestFileUploaderId = (last * 1) + 1;

JavaScript uses the + operator to concatenate strings which happens in your case. JavaScript使用+运算符来连接在您的情况下发生的字符串。 Only numerical types are summarized arithmetically. 算术上只汇总了数字类型。 In order to increment the ID you have to cast it to an integer before. 为了增加ID,您必须先将其转换为整数。

console.log ('1' + 1 ); 
String '11'

console.log( parseInt('1') + 1 )
Int 2

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

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