问题
This question already has an answer here:
- How to force JS to do math instead of putting two strings together 9 answers
I\'m trying to add all of the calorie contents in my javascript like this:
$(function() {
var data = [];
$( \"#draggable1\" ).draggable();
$( \"#draggable2\" ).draggable();
$( \"#draggable3\" ).draggable();
$(\"#droppable_box\").droppable({
drop: function(event, ui) {
var currentId = $(ui.draggable).attr(\'id\');
var total = 0;
data.push($(ui.draggable).attr(\'id\'));
if(currentId == \"draggable1\"){
var myInt1 = parseFloat($(\'#MealplanCalsPerServing1\').val());
}
if(currentId == \"draggable2\"){
var myInt2 = parseFloat($(\'#MealplanCalsPerServing2\').val());
}
if(currentId == \"draggable3\"){
var myInt3 = parseFloat($(\'#MealplanCalsPerServing3\').val());
}
if ( typeof myInt1 === \'undefined\' || !myInt1 ) {
myInt1 = parseInt(0);
}
if ( typeof myInt2 === \'undefined\' || !myInt2){
myInt2 = parseInt(0);
}
if ( typeof myInt3 === \'undefined\' || !myInt3){
myInt3 = parseInt(0);
}
total = parseFloat(myInt1 + myInt2 + myInt3);
$(\'#response\').append(total);
}
});
$(\'#myId\').click(function(event) {
$.post(\"process.php\", ({ id: data }), function(return_data, status) {
alert(data);
//alert(total);
});
});
});
Instead of adding the variables they get concatenated. I\'ve tried using parseInt, parseFloat, and Number but I still just get concatenation and not addition. Please look at the view source at http://maureenmoore.com/momp_112412/121912_800.html
回答1:
Your code concatenates three strings, then converts the result to a number.
You need to convert each variable to a number by calling parseFloat()
around each one.
total = parseFloat(myInt1) + parseFloat(myInt2) + parseFloat(myInt3);
回答2:
Should also be able to do this:
total += eval(myInt1) + eval(myInt2) + eval(myInt3);
This helped me in a different, but similar, situation.
回答3:
The following statement appends the value to the element with the id of response
$('#response').append(total);
This makes it look like you are concatenating the strings, but you aren't, you're actually appending them to the element
change that to
$('#response').text(total);
You need to change the drop event so that it replaces the value of the element with the total, you also need to keep track of what the total is, I suggest something like the following
$(function() {
var data = [];
var total = 0;
$( "#draggable1" ).draggable();
$( "#draggable2" ).draggable();
$( "#draggable3" ).draggable();
$("#droppable_box").droppable({
drop: function(event, ui) {
var currentId = $(ui.draggable).attr('id');
data.push($(ui.draggable).attr('id'));
if(currentId == "draggable1"){
var myInt1 = parseFloat($('#MealplanCalsPerServing1').val());
}
if(currentId == "draggable2"){
var myInt2 = parseFloat($('#MealplanCalsPerServing2').val());
}
if(currentId == "draggable3"){
var myInt3 = parseFloat($('#MealplanCalsPerServing3').val());
}
if ( typeof myInt1 === 'undefined' || !myInt1 ) {
myInt1 = parseInt(0);
}
if ( typeof myInt2 === 'undefined' || !myInt2){
myInt2 = parseInt(0);
}
if ( typeof myInt3 === 'undefined' || !myInt3){
myInt3 = parseInt(0);
}
total += parseFloat(myInt1 + myInt2 + myInt3);
$('#response').text(total);
}
});
$('#myId').click(function(event) {
$.post("process.php", ({ id: data }), function(return_data, status) {
alert(data);
//alert(total);
});
});
});
I moved the var total = 0;
statement out of the drop event and changed the assignment statment from this
total = parseFloat(myInt1 + myInt2 + myInt3);
to this
total += parseFloat(myInt1 + myInt2 + myInt3);
Here is a working example http://jsfiddle.net/axrwkr/RCzGn/
来源:https://stackoverflow.com/questions/13953939/how-to-force-addition-instead-of-concatenation-in-javascript