我正在尝试构建一个函数,该函数将触发 focusin / focusout 并<span>
在我在输入字段中输入数据时显示结果。
我的 doc-ready 函数在.<head>
之后的脚本标记中调用下面的代码<body>
。
这是一个屏幕截图: 这是输入字段和显示结果的跨度
代码:
var $costPrice = parseInt($("#cost-price").val()); // Cost price <div>
var $markupPercentage = parseInt($("#markup-percentage").val()); // Markup <div>
var $sellingPrice = ($markupPercentage / 100) * $costPrice + $costPrice;
var $finalPrice = $("#final-price"); // This <span> tag that receive the result of $sellingPrice
function showPrice($sellingPrice, $finalPrice){
if ($sellingPrice !== null) {
$finalPrice.html("$" + $sellingPrice);
} else {
$finalPrice.html("$0"); // Here I want to replace Nan by $0
}
};
$costPrice.focusout(showPrice); // I want to trigger/show the result.
$markupPercentage.focusout(showPrice); // I want to trigger/show the result.
如果我在输入中输入一个值并在控制台中运行下面的代码,它就可以工作。但这不是交互式的。我想获得相同的结果,但在 focusin / focusout 输入字段上。
var $costPrice = parseInt($("#cost-price").val()); // Cost price <div>
var $markupPercentage = parseInt($("#markup-percentage").val()); // Markup <div>
var $sellingPrice = ($markupPercentage / 100) * $costPrice + $costPrice;
var $finalPrice = $("#final-price").html("$" + $sellingPrice);
感谢帮助 !