2 回答

TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超7個(gè)贊
這是一個(gè)干版本
const num = str => isNaN(str) || str.trim() === "" ? 0 : +str;
const srt_gew = num(document.getElementById('add_calc_srt_gew').value),
dia_inner = num(document.getElementById('add_calc_dia_inner').value),
dia_out = num(document.getElementById('add_calc_dia_out').value),
breedte = num(document.getElementById('add_breedte').value);
if (srt_gew && dia_inner && dia_out && breedte && dia_out > dia_inner) { /* do something */ }

TA貢獻(xiàn)1993條經(jīng)驗(yàn) 獲得超6個(gè)贊
您正在比較字符串中的 2 個(gè)數(shù)字。它會(huì)導(dǎo)致意想不到的行為,如'9'
> '11'
。你必須先給parseInt()
他們。嘗試這個(gè):
... && parseInt(document.getElementById('add_calc_dia_out').value) > parseInt(document.getElementById('add_calc_dia_inner').value)) {
此外,您應(yīng)該在進(jìn)行比較之前檢查它是否是有效數(shù)字:
!Number.isNaN(document.getElementById('add_calc_dia_out').value) && !Number.isNaN(document.getElementById('add_calc_dia_inner').value))
添加回答
舉報(bào)