If statement in jquery not working

alert (x) - false. But for some reason, this is not included in the if statement? Any ideas?

HTML

 @{bool x = false;
            foreach (var c in Model.Cleaner.TimeConfirmations.Where(l => l.date.ToShortDateString() == DateTime.Now.ToShortDateString() || l.date.ToShortDateString() == DateTime.Now.AddDays(1).ToShortDateString()))
            {
                     x = true;
            }
            <span class="ifAvailable" data-confirmationchecker="@x" value="15">@x</span>
           }

Jquery

var x = $(".ifAvailable").data('confirmationchecker')
alert(x);
if ( x == false) {
    alert("hi")
}
+4
source share
3 answers

Data attributes can only contain strings :

The data- * attributes are composed of two parts:

  • The attribute name must not contain capital letters and must be at least one character after the prefix "data -"
  • Attribute value can be any string

So, you are comparing the string "false" with Boolean false, which do not match.

Instead

if (x == false)

using

if (x == "false")

Or you can use this technique :

var x = ($(".ifAvailable").data('confirmationchecker') == "true");
alert(x);
if ( x == false) {
    alert("hi")
}
+6
source

x , ,

var x = $(".ifAvailable").data('confirmationchecker')
alert(x);
if (JSON.parse(x) == false) {
    alert("hi")
} 

boolean JavaScript?

+1

X seems to be the string type "false", try:

if ( x == "false") {
    alert("hi")
}

or translate the variable x to type bool.

0
source

Source: https://habr.com/ru/post/1662202/


All Articles