亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

根據匹配數字刪除/添加類別

根據匹配數字刪除/添加類別

繁星coding 2023-08-24 15:32:16
當地址字段中沒有數字時,我正在努力刪除/添加類。當字段中沒有數字時,類:“ok-form”將被刪除,類:“error-form”必須添加。如果我只是$(this).removeClass('ok-form').addClass('error-form');在這部分(第 12 行)之后添加:if (!$(this).val().match(/\d+/)) {它不起作用。有人有想法嗎? $('input[name="shipping_address[address1]"], input[name="payment_address[address1]"]').on('blur', function() {        $(this).removeClass('ok-form error-form');        if ($(this).siblings('.supercheckout-required').css('display') == "none" && $(this).val() == '') {            $(this).removeClass('ok-form error-form');        } else if ($(this).val() == '') {            $(this).removeClass('ok-form').addClass('error-form');            $(this).parent().append('<span class="errorsmall">' + required_error + '</span>');        } else if (!validateAddress($(this).val())) {            $(this).removeClass('ok-form').addClass('error-form');            $(this).parent().append('<span class="errorsmall">' + invalid_address + '</span>');        } else if (validateAddress($(this).val())) {            if (!$(this).val().match(/\d+/)) {                if (!$(this).parent().find('.warningsmall').length)                    $(this).parent().append('<span class="warningsmall">' + street_number_warning + '</span>');            } else {                $(this).parent().find('.warningsmall').remove();            }            $(this).removeClass('error-form').addClass('ok-form');        }    });
查看完整描述

1 回答

?
月關寶盒

TA貢獻1772條經驗 獲得超5個贊

該塊中的最后一行刪除了錯誤表單類,并將 OK 表單類添加回來,使您嘗試添加的行本質上是無操作:


else if (validateAddress($(this).val())) {

? ? if (!$(this).val().match(/\d+/)) {

? ? ? ? // we try swapping classes

? ? ? ? $(this).removeClass('ok-form').addClass('error-form');

? ? ? ? if (!$(this).parent().find('.warningsmall').length)

? ? ? ? ? ? $(this).parent().append('<span class="warningsmall">' + street_number_warning + '</span>');

? ? } else {

? ? ? ? $(this).parent().find('.warningsmall').remove();

? ? }

? ? // this line undoes the class changes

? ? $(this).removeClass('error-form').addClass('ok-form');

}

事實上,如果您在調試器中單步執行代碼,您將看到類切換,然后在到達塊末尾時切換回來。

有很多方法可以解決這個問題。一種方法是在該塊中保留一個布爾值,然后根據最后的值設置類:

else if (validateAddress($(this).val())) {

? ? let isErrorState = false;

? ? if (!$(this).val().match(/\d+/)) {

? ? ? ? isErrorState = true;

? ? ? ? if (!$(this).parent().find('.warningsmall').length)

? ? ? ? ? ? $(this).parent().append('<span class="warningsmall">' + street_number_warning + '</span>');

? ? } else {

? ? ? ? $(this).parent().find('.warningsmall').remove();

? ? }


? ? // swap classes


? ? if (isErrorState) {

? ? ? ? $(this).removeClass('ok-form').addClass('error-form');

? ? }

? ? else {

? ? ? ? ?$(this).removeClass('error-form').addClass('ok-form');

? ? }

}


查看完整回答
反對 回復 2023-08-24
  • 1 回答
  • 0 關注
  • 206 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號