简体   繁体   中英

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.

the div is

<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".

I tried to use "latestFileUploaderId++" which giving me right result once as "1" and after going as "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). try using 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. Instead, multiplying by 1 is much faster, so something like this is what I'd recommend:

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. Only numerical types are summarized arithmetically. In order to increment the ID you have to cast it to an integer before.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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