2016-03-01 59 views
-2

請問我該如何在Jquery中完成此工作?我想要實現這個使用jQuery需要你的幫助..例如,如果35000 - 349990 = - 314990Jquery如果值爲負主值

如果顯示-314990我想顯示錯誤給用戶...

我想出了這個沒「T幫...

$("#apaid1").keyup(function() { // apaid input id for user to enter amount 
    var textinput = $('#apaid1').val(); // get value from input id apaid1 
    var textinput1 = $('#epayment1').val(); // get value from input id epayment1 

    $("#bl1").val("" + textinput1 - textinput); 
    // input id bl1 used as output 

    if ($("#bl1").val() < textinput1) { 

     $("#bl1").val("" + textinput1 - textinput); 
    }; 
}); 

這是它給了我我-314990我想顯示負值

對不起,英文不好的錯誤基地。 非常感謝你....

+0

顯示錯誤在哪裏? –

+0

嘗試一下:'if(a + b <0){/ * Do stuff * /}' –

+0

顯示錯誤@'#bl1'輸入ID –

回答

0
$("#apaid1").keyup(function(){ 
    var textinput = $('#apaid1').val(); 
    var textinput1 = $('#epayment1').val(); 

    /* I'm assuming that the "#bl1" tag is the input where you want to store the product of "textinput" and "textinput1"? */ 
    $("#bl1").val(textinput1 - textinput); 

    //if the value is less than zero (e.g: a negative number), send an alert to the user. 
    if ($("#bl1").val() < 0) { 
     alert("Hey! Your answer resulted in a negative. Try again"); 
    }; 

}); 

我不完全知道爲什麼你設定條件爲 「如果($(」 #BL1 「)。VAL()< textinput1)......」 和爲什麼當你滿足這個條件時你試圖重新計算價值。這對我來說似乎很奇怪。所以,如果你有這樣做的理由,或者如果我誤解了你的問題,請詳細說明。

+0

https://jsfiddle.net/marshallunduemi/gkd7yLuc/4/這裏是JSfidle –

+0

** Aetheus **和** Asad Sheikh **它工作正常JSfiddle更新https://jsfiddle.net/marshallunduemi/gkd7yLuc/6 /謝謝大家,非常高興 –

+0

請參閱:https://jsfiddle.net/ewgwc52t/ 如果這是一項家庭作業,我強烈建議您在基礎JavaScript和jQuery在前進之前。 – aetheus

0

使用此條件檢查負值。

($("#bl1").val() < 0) 
+0

謝謝你,真的很感激 –

0

我已經加入

if(textinput1 - textinput < 0) 
{ 
    alert("Error: Negative"); 
} 

看到更新後的jsfiddle