gpt4 book ai didi

javascript - 如何在页面加载时初始化 keyup 函数

转载 作者:行者123 更新时间:2023-11-29 10:12:49 25 4
gpt4 key购买 nike

我有以下内容

$(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);
}
});

});

我想在页面加载时调用 keyup 函数,以便运行计算中的一些默认值,因为价格最初为空。

我可以做这样的事情吗?

$(document).ready(function () {
window.onload = LabourPrice;
....
}

那我怎么包装 LabourPrice 呢?

谢谢

编辑

按要求添加了功能细节

最佳答案

我认为 window.onload 是多余的,因为它是 ready 的一部分,需要在 ready 函数中设置事件。为什么不试试

$(document).ready(function () {
$('#ArtworkDetail_NoOfProofs').keyup(function () {
});
LabourPrice();
}
function LabourPrice() {
}

假设 LabourPrice 是一个函数(刚刚看到您的编辑)。这个函数定义可以在 ready

之外

关于javascript - 如何在页面加载时初始化 keyup 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29985678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com