简体   繁体   中英

JavaScript Declaration or statement expected.ts(1128)

My JavaScript code is giving me a "declaration or statement expected error". It brings it up on the else statement. I'm using it to make a captcha for a small html feedback form.

Javascript:

function initCaptcha() {
    var captcha = generateCaptcha(),
        captchaAns = eval(captcha);
    
    $("#captcha")
    .attr("placeholder", captcha+" = ")
    .on("keyup", function() {
    if ($(this).val() !== "" && $(this).val() == captchaAns)
        $(".btn").removeClass("btn-disable");
        $(this).addClass("correct");
    else
        $(this).removeClass("correct");
        $(".btn").addClass("btn-disable");
    });
}

CSS:

.contact-form .btn {
    width: 180px;
    background-color: transparent;
    color: #e31d1a;
    font-size: 16px;
    border: 2px solid #e31d1a;
    padding: 0;
    margin-left: auto;
    cursor: pointer;
}

.contact-form .btn:hover {
    background-color: #e31d1a;
    transition: .5s linear;
    color: #f1f1f1;
}

.btn-disable {
    opacity: 0.65;
    cursor: not-allowed;
    pointer-events: none;
}

HTML:

<input required type="text" class="textb" placeholder="Name*">
<input required type="text" class="textb" placeholder="Phone Number*">
<input required type="text" class="textb" placeholder="Email*">
<textarea required placeholder="Tell Us How We Can Help*"></textarea>
<input required type="text" id="captcha" class="textb">
<input required type="submit" class="btn btn-disable" value='Send

try this out, the error caused due to missing braces

if ($(this).val() !== "" && $(this).val() == captchaAns) {
    $(".btn").removeClass("btn-disable");
    $(this).addClass("correct");
  } else {
    $(this).removeClass("correct");
    $(".btn").addClass("btn-disable");
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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