简体   繁体   English

js变量和单选按钮已选中/未选中

[英]js variable and radio button checked / unchecked

<input id='rb1' type="radio" name="bafter" checked>
<input id='rb2' type="radio" name="bafter">

js js

$(".item").click(function(){
    console.log(a); // 39 (that's ok)
    if ($('#rb1').prop('checked') == true) {a -=1;}
    else {a +=1;}
});

console.log(a);

If rb1 checked result is - 38 (ok) 如果rb1检查的结果是-38(确定)
If rb2 is not checked reusult is - 391 (should be 40) 如果未检查rb2,则rb2为-391(应为40)

Any help? 有什么帮助吗?

It is concatenating as a string. 它串联为一个字符串。 Try a =eval("a+1") 尝试a =eval("a+1")

a might be getting evaluated as a string. 一个可能被评估为字符串。
And string + int = string in Javascript string + int = Javascript中的string
So, "39" + 1 = "391" 因此,“ 39” +1 =“ 391”
Use a = parseInt(a) + 1; 使用a = parseInt(a) + 1; and a = parseInt(a) - 1; a = parseInt(a) - 1;

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

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