我想使用javascript在可编辑控件中显示{00.00}这种格式?有谁知道ans?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我想使用javascript在可编辑控件中显示{00.00}这种格式?有谁知道ans?相关的知识,希望对你有一定的参考价值。
无论输入什么数字,我想要{00.00}这种格式弹出时,有些我怎么没有得到它背后的功能或逻辑:
$('#EstimatedHours').editable({
showbuttons: false,
unsavedclass: null,
type: 'text',
inputclass: 'estimatedhours-select',
mode: 'popup',
emptytext: 'Hours',
clear: false,
url: function (params) {
var url = '@Url.Action("OnEstimatedHoursChange", "MyTasks")';
var taskId = $("#SelectedTaskID").val();
var EstHours = params.value
var model = {
"TaskID": taskId,
"EstimatedHours": params.value
}
debugger;
$("#dvLoading").show();
$.ajax({
url: url,
data: model
}).done(function (e) {
debugger;
$("#EstimatedHours").text($('.estimatedhours-select').val());
$("#dvLoading").hide();
toastr.success('Hours updated successfully.', 'Success', { timeOut: 5000, positionClass: "toast-bottom-right", closeButton: true })
var taskid = $("#SelectedTaskID").val();
$("#MainTaskHour-" + taskid).text(EstHours);
$("#divTaskLatestEvents").html(e);
document.getElementById('EstimatedHours').innerHTML = EstHours;//parseFloat(Math.round(EstHours * 100) / 100).toFixed(2);
});
},
答案
这是一个关于如何格式化输入的基本示例,因此它看起来像您想要的{00.00}
格式。
function onInputHandler(event) {
const
inputValue = sanatizeInput(event.target.value),
outputElement = document.getElementById('myOutput');
if (outputElement !== null) {
outputElement.textContent = formatInputAsTime(inputValue);
}
}
function sanatizeInput(value) {
// Return the provided value after replacing everything which
// isn't a digit with an empty string. The resulting string
// will only contain digits.
return value.replace(/D/g, '');
}
function formatInputAsTime(value) {
const
// Make sure the string is always at least 4 characters long.
paddedValue = value + '0000',
// Take the first 4 characters from the string, this will contain
// the user's input padded with extra zeroes as needed.
displayValue = paddedValue.substr(0, 4);
// Take the first 2 chars, add a dot and add the last 2 characters.
return `${displayValue.substr(0, 2)}.${displayValue.substr(-2)}`;
}
// Get the input element from the DOM and listen to its change event.
const
inputElement = document.getElementById('myInput');
if (inputElement !== null) {
inputElement.addEventListener('input', onInputHandler);
}
// This will print 10.00
console.log(formatInputAsTime('1'));
// This will print 12.30
console.log(formatInputAsTime('123'));
// This will print 12.34
console.log(formatInputAsTime('12345'));
.output {
color: royalblue;
font-family: monospace;
font-size: 1.5em;
margin-top: .5em;
}
<input type="text" id="myInput" placeholder="Enter time">
<p>
formatted output:
</p>
<div id="myOutput" class="output"></div>
以上是关于我想使用javascript在可编辑控件中显示{00.00}这种格式?有谁知道ans?的主要内容,如果未能解决你的问题,请参考以下文章
在网格视图中的 ASCX 控件内的控件上使用 Javascript 显示隐藏。 (ASP.NET + Javascript)