1
このコードはFireFoxではうまく動作しますが、多数の場合、この結果はChromeやEdgeでは表示されず、コンソールにもエラーはありません。なぜこれがFireFoxで特別に機能するのか?2つのフォーマットされた大きな数字を掛け合わせるとChromeに結果は表示されません
<html>
<head>
<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/2.2.3/jquery.min.js'></script>
</head>
<body>
<input type="text" name="Customers2" id="Customers2" maxlength="11" class="number" value="" placeholder="Please enter a number"/>
<input type="text" name="MonetarySpend" id="MonetarySpend" maxlength="11" class="number" value="" placeholder="Please enter a dollar amount"/>
<input type="text" disabled="disabled" name="MarketSize" id="MarketSize" maxlength="11" value=""/>
<script>
$(document).ready(function()
{
$("#Customers2,#MonetarySpend").on("change", function()
{
/* http://stackoverflow.com/questions/12559208/how-to-remove-comma-from-number-which-comes-dynamically-in-tpl-file */
var c2 = $("#Customers2").val();
c2 = c2.replace(/\,/g,'');
c2 = parseInt(c2,10);
var ms = $("#MonetarySpend").val();
ms = ms.replace(/\,/g,'');
ms = parseInt(ms,10);
var MarketSize = c2 * ms;
$("#MarketSize").val(MarketSize);
$('#MarketSize').val(commaSeparateNumber(MarketSize));
});
$('#MarketSize').val(commaSeparateNumber($('#MarketSize').val()));
});
/* http://stackoverflow.com/questions/2632359/can-jquery-add-commas-while-user-typing-numbers */
$('input.number').keyup(function(event) {
// skip for arrow keys
if(event.which >= 37 && event.which <= 40) return;
// format number
$(this).val(function(index, value) {
return value
.replace(/\D/g, "")
.replace(/\B(?=(\d{3})+(?!\d))/g, ",")
;
});
});
/* http://stackoverflow.com/questions/3883342/add-commas-to-a-number-in-jquery */
function commaSeparateNumber(val)
{
while (/(\d+)(\d{3})/.test(val.toString()))
{
val = val.toString().replace(/(\d+)(\d{3})/, '$1'+','+'$2');
}
return val;
}
</script>
</body>
</html>