如何添加而不是串联 [英] How to make an addition instead of a concatenation

查看:93
本文介绍了如何添加而不是串联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入按钮.我为此输入创建了一个名为"multiplicateur"的属性,其值为1.单击按钮时,触发了.click().该函数假定获取属性的值并将其加1.所以我的输出应该是2.取而代之的是输出11.看来系统是级联而不是加法.

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.

我的HTML:

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

我的JS:

$('#envoyer').click(function() {
    var ajaxData = $(this).attr('multiplicateur');
    alert(ajaxData + 1);
});

推荐答案

ajaxDatastring.因此,您需要 解析 integer ...

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

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

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

});

这篇关于如何添加而不是串联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆