设置 Text/Val 在单击/聚焦之前不会出现在文本输入中 (jQuery)
我有一个复选框,可以根据选中或未选中的情况更新页面上的总计。在我的表单的一部分,如果用户决定不需要该类别,我想将 Telerik 数字文本框(文本输入字段)清零。
当取消选中上述复选框时,文本框中的值右边应该变回零。为了实现这一点,我编写了下面的 jQuery 代码(注意,只要表单上发生更改,就会调用此函数):
var zeroValue = getNum(0.00);
//Scaffolding
if ($("#ScaffoldingRequired").attr('checked')) {
scaffoldingCost = getNum($("#ScaffoldingCost").val());
totalCost += scaffoldingCost;
} else {
$("#ScaffoldingCost").val(zeroValue);
$("#ScaffoldingCost").text(zeroValue);
}
//Round&Convert to 'decimal'
function getNum(num) {
var number = parseFloat(num) || 0.00;
var rNumber = Math.round(number * 100) / 100;
return rNumber.toFixed(2) * 1;
}
注意:为了确保一切正确舍入并且不会给出 NaN,我使用了 getNum 函数(我知道这有点黑客)。
此代码有效,并且我的所有总计计算正确,但文本框不会显示零值,直到用户实际单击输入框。我在下面显示了这个:
我可以做些什么来回收我的输入框以便立即显示零值?或者我设置的值/文本完全错误?
I have a checkbox which updates totals on a page based on whether it's checked or unchecked. On one part of my form, I would like to zero out a Telerik Numeric TextBox (Text Input field) if the user decides they do not require that category.
When the above checkbox is unchecked, the value in the textbox on the right should change back to zero. To accomplish this, I've written the jQuery code below (note, this function is called whenever something changes on the form):
var zeroValue = getNum(0.00);
//Scaffolding
if ($("#ScaffoldingRequired").attr('checked')) {
scaffoldingCost = getNum($("#ScaffoldingCost").val());
totalCost += scaffoldingCost;
} else {
$("#ScaffoldingCost").val(zeroValue);
$("#ScaffoldingCost").text(zeroValue);
}
//Round&Convert to 'decimal'
function getNum(num) {
var number = parseFloat(num) || 0.00;
var rNumber = Math.round(number * 100) / 100;
return rNumber.toFixed(2) * 1;
}
NOTE: To make sure everything rounds properly and doesn't give me NaN's, I've used my getNum function ( I understand it's a bit hack ).
This code works, and all my totals calculate properly but the textbox does not display a zero value until the user actually clicks on the input box. I've displayed this below:
Is there something I can do to recycle my input box so it displays the zero value immediately? Or am I setting the value/text completely wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您说您正在使用 Telerik,您是否考虑过使用他们的客户端 API 来更改文本框的值?
You say you're using Telerik, have you considered using their client API for changing the value of the textbox?