2017-03-21 11 views
1

我被驗證與jQuery條件相結合。有人可以給我一個很好的提示嗎?幫助表示讚賞:)只有當收音機的特定值被選中時纔有效

這是我的代碼,驗證工作正常無條件,但它應該只適用於如果其中一個無線電「shipping_method」的值被選中。

var shipping = jQuery('input[name="shipping_method"]:checked').val(); 
    if(shipping == 'matrixrate_matrixrate_2048') { 
     Validation.add('validate-alphanumeric', 'Please enter number.', function(v) { 
     return Validation.get('IsEmpty').test(v) || /[0-9]+/m.test(v); 
    }); 
}; 

也許這是錯誤的方式,我只想檢查一個數字是否添加到地址,如果條件爲真。

在此先感謝您的幫助。

+0

Please add you markup as well – KornholioBeavis

+0

您是否嘗試過比較,如'if(shipping ==='matrixrate_matrixrate_2048'){'而不是分配 – adeneo

+0

Markup?你的意思是?所以我不是程序員。 –

回答

1

如果條件是,你使用的只是一個=,當你這樣做的時候你會改變變量的值,並且返回值就是變量的新值,在這種情況下返回值將是'matrixrate_matrixrate_2048',這將會進入在你的條件。爲了比較您需要使用=====。那可行。

var shipping = jQuery('input[name="shipping_method"]:checked').val(); 
    if(shipping === 'matrixrate_matrixrate_2048') { /* <---- Here the change */ 
     Validation.add('validate-alphanumeric', 'Please enter number.', function(v) { 
     return Validation.get('IsEmpty').test(v) || /[0-9]+/m.test(v); 
    }); 
}; 
0

謝謝大家回答。 我通過在條件中使用addClass和removeClass解決它,並把驗證分開。所以現在只會驗證該類是否被添加並且工作正常。

相關問題