Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

jquery - How to make an addition instead of a concatenation

I have an input button. I created for this input an attribute called "multiplicateur" which has the value of 1. When the button is clicked I have a .click() triggered. The function is suppose to get the value of the attribute and add 1 to it. So my output should be 2. Instead the output is 11. It seems the system makes a concatenation instead of an addition.

My HTML:

<input id="envoyer" type="submit" multiplicateur=1>

My JS:

$('#envoyer').click(function() {
    var ajaxData = $(this).attr('multiplicateur');
    alert(ajaxData + 1);
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

ajaxData is a string. Thus, you need to parse it to an integer...

 $('#envoyer').click(function() {
    const ajaxData = $(this).attr('multiplicateur');

    /* parse string to integer */
    alert(parseInt(ajaxData) + 1);

});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...