このコードでは目的の出力が得られません。たとえば、100 から 199 の間で何かを入力した場合を除いて、すべてが機能しているようです。残高を両方のボックスに分割したい場合、もう一方のボックスに負の数が表示されます。ここで実際の例を見ることができます: http://jsfiddle.net/bFJq2/ (私が何を意味するかを見るために 100 を入力してください)
私が間違っていることがあるかどうかはわかりませんが、数値を小数または何かとして扱っているように感じます。どんな助けでも大歓迎です。
JS は次のとおりです。
// make sure both inputs are equal to balance
$('input[id*="amount"]').on("blur", function() {
var balance = $('#balance').val()
,thisAmount = $(this)
,otherAmount = $('input[id*="amount"]').not(this);
// check if value is less than balance
if(thisAmount.val() < balance && thisAmount.val() + otherAmount.val() <= balance){
// if so populate other input with remaining amount
otherAmount.val(balance - thisAmount.val());
}
// check to make sure amount is not greater than balance
if((thisAmount.val() + otherAmount.val()) > balance){
$('input[id*="amount"]').not(this).val(function(){
// if value is greater than balance, just split balance into both amounts
if(thisAmount.val() > balance){
thisAmount.val(balance/2);
return balance/2;
}
});
}
});