简体   繁体   English

JavaScript函数发送字符串但接收布尔值

[英]JavaScript function sending string but receiving Boolean value

I am facing the unexpected behavior of javascript function. 我正面临JavaScript函数的意外行为。 Im an passing the ID of a field as string to function but it is receiving as bool value. 我将字段的ID作为字符串传递给函数,但它作为布尔值接收。 Please help the code is below. 请帮助下面的代码。

Function 功能

 function page_smooth_scroll(target_id) { if (target_id =! null) { $j('html, body').animate({ scrollTop: $j("#" + target_id).offset().top - 120 }, 500); } } 

calling function 调用功能

 function validatePassword(){ var validPassword = false; var pwd = $j("#Password").val().trim(); var cfmPwd = $j("#ConfirmPassword").val().trim(); if((pwd == "") || (cfmPwd == "")){ $j("#ConfirmPassword").addClass("invalidPwd").nextAll("ul.err-msg").html("<li>Please enter Password</li>"); //here id is passed as string page_smooth_scroll("ConfirmPassword"); validPassword = false; } else{ $j("#ConfirmPassword").removeClass("invalidPwd").nextAll("ul.err-msg").empty(); if(pwd != cfmPwd){ $j("#ConfirmPassword").addClass("invalidPwd").nextAll("ul.err-msg").html("<li>Password does not match</li>"); //here id is passed as string page_smooth_scroll("ConfirmPassword"); validPassword = false; } else{ $j("#ConfirmPassword").removeClass("invalidPwd").nextAll("ul.err-msg").empty(); validPassword = true; } } return validPassword; } 

the image is below while debugging passing string 该图像在调试传递字符串时在下面

receiving bool 接收布尔

if (target_id =! null) {

I think you mean a != b 我想你是说a != b

Because a=!b means a = !b which means "assign the opposite boolean value", which will indeed turn anything into a boolean. 因为a=!b意味着a = !b ,这意味着“分配相反的布尔值”,这实际上会将任何内容转换为布尔值。

Next time if you think a function is "receiving a boolean", make sure to debug the value before running any statements. 下次如果您认为某个函数“正在接收布尔值”,请确保在运行任何语句之前调试该值。 I'm sure it's still a string when going into the function. 我确定进入函数时它仍然是字符串。

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

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