jquery 和 JavaScript。我真的很感激任何建议。我想在文本字段中输入一些内容后实现像文本字段 onfocusout 一样,如果它只是一个数字,它会在最后自动添加 .00,但是如果他们输入像 2.00 这样的内容,它就不会再添加 .00 了。
<!DOCTYPE html>
<html>
<script type="text/javascript">
$('#tx').on('focusout', function() {
var x=document.getElementById(tx).value;
if (isNaN(x)) // this is the code I need to change
{
$(this).val(`${$(this).val()}`);
}else{
$(this).val(`${$(this).val()}.00`);
});
</script>
</html>
最佳答案
假设设置了tx
<script type="text/javascript">
$('#tx').on('focusout', function() {
var x=document.getElementById(tx).value;
$(this).val(parseInt(x.replace(/[^0-9\.]+/g,'')||0).toFixed(2));
});
</script>
关于javascript - 焦点文本字段检查它是否是数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51723334/