admin管理员组文章数量:1379395
I'm trying to do something but im struggling with a stupid bug, hope you can help me.
As you can see in my code I have on server some check box and I attached to it a JavaScript function. This function gets two values (current value and original value).
My problem is when this function triggers on the if
it always get to the first clause and never reach the else clause and it doesn't make any sense.
When I used few alerts in my code this is what happens:
case 1: alert #1 original: false
current: true alert #2 true
case 2: alert #1 original: false
current: false alert #2 true
As you can see, no matter what I get true on the 2nd alert.
Hope I managed to explain my problem.
<asp:CheckBox id="chkIsCustomQuantity" originalvalue="false" runat="server" onclick="checkChange(this.checked, this.attributes['originalvalue'].value" />
<script type="text/javascript" language="javascript">
function checkChange(value, originalValue) {
//i added this for debugging purpose
alert('original: ' + originalValue + ' ' + 'current: ' + value);
alert(value != originalValue);
if (value != originalValue) {
// always happens
}
else {
// never happens
}
}
</script>
I'm trying to do something but im struggling with a stupid bug, hope you can help me.
As you can see in my code I have on server some check box and I attached to it a JavaScript function. This function gets two values (current value and original value).
My problem is when this function triggers on the if
it always get to the first clause and never reach the else clause and it doesn't make any sense.
When I used few alerts in my code this is what happens:
case 1: alert #1 original: false
current: true alert #2 true
case 2: alert #1 original: false
current: false alert #2 true
As you can see, no matter what I get true on the 2nd alert.
Hope I managed to explain my problem.
<asp:CheckBox id="chkIsCustomQuantity" originalvalue="false" runat="server" onclick="checkChange(this.checked, this.attributes['originalvalue'].value" />
<script type="text/javascript" language="javascript">
function checkChange(value, originalValue) {
//i added this for debugging purpose
alert('original: ' + originalValue + ' ' + 'current: ' + value);
alert(value != originalValue);
if (value != originalValue) {
// always happens
}
else {
// never happens
}
}
</script>
Share
Improve this question
edited Feb 19, 2012 at 21:49
user1106925
asked Feb 19, 2012 at 21:31
PopokokoPopokoko
6,54316 gold badges50 silver badges58 bronze badges
2
- 1 Use console.log and/or your debugger to inspect variable values. Trust me, you will never want to go back to alert() after that. – hugomg Commented Feb 19, 2012 at 21:37
- Unless you're a 13-year-old girl, "u" is not a word. Please use proper English. – user1106925 Commented Feb 19, 2012 at 21:37
1 Answer
Reset to default 6originalValue
is always a string. value
is a boolean. So, they're never equal, but when you print them out they appear identical (since they're both converted to the same string value at that point).
You can fix this in a variety of ways. One simple way is to convert both to strings first:
if (String(value) != String(originalValue))
本文标签: if statementJavascript comparing two values always not equalStack Overflow
版权声明:本文标题:if statement - Javascript: comparing two values always not equal - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744501343a2609341.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论