2015-04-28 64 views
-1

問題是我有一個JavaScript,看着幾個文本字段,並獲取每個的值。將它們加在一起,並在我的廢文本字段中給我一個總數。我想要做的是說,如果設置是A B C或D做總和,如果安裝程序的值是空白做其他。它似乎不想工作任何幫助將不勝感激。想不通爲什麼我的JavaScript不工作

$(document).ready(function(){ 

    $('.scrap').click(function() { 
    var weld_scrap = $('.weld').val(); 
    var dim_scrap = $('.dim').val(); 
    var visual_scrap = $('.visual').val(); 
    var adjust_scrap = $('.adjust').val(); 
    var setup = $('.setup').val(); 
    console.log(setup) 
    var sum = parseInt(weld_scrap) + parseInt(dim_scrap) + parseInt(visual_scrap) + parseInt(adjust_scrap) + parseInt(setup); 
    var other = parseInt(weld_scrap) + parseInt(dim_scrap) + parseInt(visual_scrap) + parseInt(adjust_scrap); 
    var A = '10'; 
    var B = '10'; 
    var C = '10'; 
    var D = '10'; 
    if (setup == 'A') { 
     setup = A; 
     $('.scrap').val(sum); 

    }else if (setup == 'B') { 
     setup = B; 
     $('.scrap').val(sum); 

    }else if (setup == 'C') { 
     setup = C; 
     $('.scrap').val(sum); 

    }else if (setup == 'D') { 
     setup = D; 
      $('.scrap').val(sum); 
    }else if (setup == '') { 
     $('.scrap').val(other); 

    } 
    }); 
}); 

我也試過這樣的,如果設置爲空

$(document).ready(function(){ 

    $('.scrap').click(function() { 
    var weld_scrap = $('.weld').val(); 
    var dim_scrap = $('.dim').val(); 
    var visual_scrap = $('.visual').val(); 
    var adjust_scrap = $('.adjust').val(); 
    var setup = $('.setup').val(); 
    console.log(setup) 
    var sum = parseInt(weld_scrap) + parseInt(dim_scrap) + parseInt(visual_scrap) + parseInt(adjust_scrap) + parseInt(setup); 
    var other = parseInt(weld_scrap) + parseInt(dim_scrap) + parseInt(visual_scrap) + parseInt(adjust_scrap); 
    var A = '10'; 
    var B = '10'; 
    var C = '10'; 
    var D = '10'; 
    if (setup == 'A') { 
     setup = A; 
     $('.scrap').val(sum); 

    }else if (setup == 'B') { 
     setup = B; 
     $('.scrap').val(sum); 

    }else if (setup == 'C') { 
     setup = C; 
     $('.scrap').val(sum); 

    }else if (setup == 'D') { 
     setup = D; 
      $('.scrap').val(sum); 
    }else if (setup === null) { 
     $('.scrap').val(other); 

    } 
    }); 
}); 

,這也

$(document).ready(function(){ 

    $('.scrap').click(function() { 
    var weld_scrap = $('.weld').val(); 
    var dim_scrap = $('.dim').val(); 
    var visual_scrap = $('.visual').val(); 
    var adjust_scrap = $('.adjust').val(); 
    var setup = $('.setup').val(); 
    console.log(setup) 
    var sum = parseInt(weld_scrap) + parseInt(dim_scrap) + parseInt(visual_scrap) + parseInt(adjust_scrap) + parseInt(setup); 
    var other = parseInt(weld_scrap) + parseInt(dim_scrap) + parseInt(visual_scrap) + parseInt(adjust_scrap); 
    var A = '10'; 
    var B = '10'; 
    var C = '10'; 
    var D = '10'; 
    if (setup == 'A') { 
     setup = A; 
     $('.scrap').val(sum); 

    }else if (setup == 'B') { 
     setup = B; 
     $('.scrap').val(sum); 

    }else if (setup == 'C') { 
     setup = C; 
     $('.scrap').val(sum); 

    }else if (setup == 'D') { 
     setup = D; 
      $('.scrap').val(sum); 
    }else if (setup == '0') { 
     $('.scrap').val(other); 

    } 
    }); 
}); 

這也太...

$(document).ready(function(){ 

    $('.scrap').click(function() { 
    var weld_scrap = $('.weld').val(); 
    var dim_scrap = $('.dim').val(); 
    var visual_scrap = $('.visual').val(); 
    var adjust_scrap = $('.adjust').val(); 
    var setup = $('.setup').val(); 
    console.log(setup) 
    var sum = parseInt(weld_scrap) + parseInt(dim_scrap) + parseInt(visual_scrap) + parseInt(adjust_scrap) + parseInt(setup); 
    var other = parseInt(weld_scrap) + parseInt(dim_scrap) + parseInt(visual_scrap) + parseInt(adjust_scrap); 
    var A = '10'; 
    var B = '10'; 
    var C = '10'; 
    var D = '10'; 
    if (setup == 'A') { 
     setup = A; 
     $('.scrap').val(sum); 

    }else if (setup == 'B') { 
     setup = B; 
     $('.scrap').val(sum); 

    }else if (setup == 'C') { 
     setup = C; 
     $('.scrap').val(sum); 

    }else if (setup == 'D') { 
     setup = D; 
      $('.scrap').val(sum); 
    }else if (setup === undefined) { 
     $('.scrap').val(other); 

    } 
    }); 
}); 
+0

可以添加一個演示? – Mritunjay

+0

您可以使用https://jsfiddle.net/進行演示 – Reeno

+0

「似乎不想工作」是什麼意思? – entropic

回答

0

您可以檢查是否爲空值從一開始,否則檢查,如果它等於「A」或「B」 ......

if(setup.match(/\S/)){ 
    if (setup == 'A') { 
    setup = A; 
     $('.scrap').val(sum); 

    }else if (setup == 'B') { 
    setup = B; 
     $('.scrap').val(sum); 

    }else if (setup == 'C') { 
    setup = C; 
     $('.scrap').val(sum); 

    }else if (setup == 'D') { 
    setup = D; 
     $('.scrap').val(sum); 
    } 
}else{ 
    // will come here when there is an empty value 
    $('.scrap').val(other); 
} 
+0

感謝您的回答,但不幸的是,如果安裝程序中有某些內容,它仍然不能正常工作。如果它是空白的,它也不會得到相同的總和值。 @ mohamed-ibrahim – Snowman1234

+0

@ Snowman1234我修改了代碼有一個額外的!在我的代碼中,讓我解釋一下這裏會發生什麼,\ S匹配任何非空白字符,所以如果它的空值,那麼它將不匹配,因此會去其他地方。 –

+0

工作你真棒謝謝你的幫助! – Snowman1234

-1

檢查,如果你有導入正確的jquery頭文件。雖然您可以使用.on()方法並檢查它.. 使用console.log()進行開發時,從start開始一步一步檢查是很好的。

$(".scrap").on("click", function() { 
    // your stuff here 
    ); 
}); 
相關問題