所以希望能够对此有所了解,因为我已经好几个小时没有运气了
我创建了4个输入字段,前三个字段乘以最终字段显示的总数。我能够格式化逗号,让字段成倍增加并生成总数。我的问题是我无法在第4个总字段中显示逗号,如下所示。任何帮助都会非常感激,只是不知道我搞砸了哪里也可能有什么不同或更好,因为我对此很新。
由于
$(document).ready(function() {
$(".input").keyup(function() {
var val1 = +$(".value1").val().replace(/,/g, "");
var val2 = +$(".value2").val().replace(/,/g, "");
var val3 = +$(".value3").val().replace(/,/g, "");
$("#result").val(val1 * val2 * val3);
});
});
$(document).on('keyup', '.comma', function() {
var x = $(this).val();
$(this).val(x.toString().replace(/,/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ","));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div>
<label for="a">Number of people:</label>
<br>
<input type="text" class="input value1 comma" />
<br>
<label for="a">Minutes Per Day:</label>
<br>
<input type="text" class="input value2 comma" />
<br>
<label for="b">Number of Days:</label>
<br>
<input type="text" class="input value3 comma" />
<br>
<label for="total">Total Minutes:</label>
<br>
<input type="text" id="result" />
</div>
答案 0 :(得分:0)
$(document).ready(function() {
$(".input").keyup(function() {
var val1 = +$(".value1").val().replace(/,/g, "");
var val2 = +$(".value2").val().replace(/,/g, "");
var val3 = +$(".value3").val().replace(/,/g, "");
var total = (val1 * val2 * val3);
$("#result").val(total.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","));
});
});
$(document).on('keyup', '.comma', function() {
var x = $(this).val();
$(this).val(x.toString().replace(/,/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ","));
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div>
<label for="a">Number of people:</label>
<br>
<input type="text" class="input value1 comma" />
<br>
<label for="a">Minutes Per Day:</label>
<br>
<input type="text" class="input value2 comma" />
<br>
<label for="b">Number of Days:</label>
<br>
<input type="text" class="input value3 comma" />
<br>
<label for="total">Total Minutes:</label>
<br>
<input type="text" id="result" />
</div>
&#13;
你应该将你的替换应用于总数