[英]How to initialise keyup function on page load
I have the following 我有以下内容
$(document).ready(function () {
//window.onload = LabourPrice;
//Control Proofing Time and LabourCost
$('#ArtworkDetail_NoOfProofs').keyup(function () {
function LabourPrice() {
var time = "@Model.ArtworkDetail.ProofType.DefaultProofTime".split(':');
var seconds = (+time[0]) * 60 * 60 + (+time[1]) * 60 + (+time[2]);
var newSeconds = seconds * $('#ArtworkDetail_NoOfProofs').val();
var date = new Date(newSeconds * 1000);
var hh = date.getUTCHours();
var mm = date.getUTCMinutes();
var ss = date.getSeconds();
var hourlyLabour = $('#LabourCostCentrePrice').val();
hourlyLabour = hourlyLabour.split('£');
var costPerSecond = hourlyLabour[1] / 3600;
var calculateCost = costPerSecond * newSeconds;
//alert("£"+calculateCost.toFixed(2));
$('#ArtworkDetail_ProofingLabourCost').val("£" + calculateCost.toFixed(2));
// If building a timestamp instead of a duration, you would uncomment the following line to get 12-hour (not 24) time
if (hh > 12) { hh = hh % 12; }
// Ensure each element has two-digits
if (hh < 10) { hh = "0" + hh; }
if (mm < 10) { mm = "0" + mm; }
if (ss < 10) { ss = "0" + ss; }
// Format your to HH:MM:SS
var t = hh + ":" + mm + ":" + ss;
$('#ArtworkDetail_ProofType_DefaultProofTime').val(t);
}
});
});
I would like to call the keyup function on page load in order to run some default value I have within a calculation, as the price initially starts off as empty. 我想在页面加载时调用keyup函数,以便在计算中运行一些默认值,因为价格最初从空开始。
Could I do something like this? 我可以这样做吗?
$(document).ready(function () {
window.onload = LabourPrice;
....
}
Then how would I wrap LabourPrice? 然后我将如何包装LabourPrice?
Thanks 谢谢
EDIT 编辑
Added Function Detail as requested 根据要求添加了功能细节
I believe the window.onload
is redundant as that is part of ready
and the event needs to be set inside the ready
function. 我相信
window.onload
是多余的,因为它是ready
一部分,并且事件需要在ready
函数内设置。 Why not try 为什么不试试
$(document).ready(function () {
$('#ArtworkDetail_NoOfProofs').keyup(function () {
});
LabourPrice();
}
function LabourPrice() {
}
Assuming that LabourPrice
is a function (just seen your edit). 假设
LabourPrice
是一个函数(刚看到你的编辑)。 This function definition can go outside the ready
此功能定义可以在
ready
之外
LabourPrice
should be outside of the keyup
event. LabourPrice
应该在keyup
事件之外。 As you have it now, LabourPrice
only exists within the keyup event, but you want to call it from the keyup event: 正如您现在所拥有的那样,
LabourPrice
仅存在于keyup事件中,但您想要从keyup事件中调用它:
$(window).load(function(){
$('#ArtworkDetail_NoOfProofs').keyup(function () {
LabourPrice();
}
}
function LabourPrice(){ ... }
Here's a good source on how window.load is different than document.ready, although this is not your main issue. 这里有一个关于window.load与document.ready不同的一个很好的来源 ,虽然这不是你的主要问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.