我对JavaScript的了解很少,因此,感谢您对此查询的帮助。
我正在学习Liquid,并使用Shopify制作一种财务计算器;我想将产品价格分配为JavaScript中的变量,因此当用户更改还款月数时,它将动态更改数据。
我已经从模板复制了大多数JavaScript。
目前,我只是使用html上的价格输入价格,但是我想知道是否有办法为屏幕上的文本分配一个javascript变量。
例如,我可以通过添加以下内容来显示产品价格:
<p>{{ price}}</p>
但是当我尝试制作一个JavaScript变量时,它是:
var amount = document.getElementById("amount");
它不会拖延任何事情。
我希望变量“金额”等于产品价格。
我的代码是...
Java脚本
<script type="text/javascript">
function calculate() {
//Look up the input and output elements in the document
var amount = document.getElementById("amount");
var period = document.getElementById("period");
var payment = document.getElementById("payment");
var total = document.getElementById("total");
var financecost = document.getElementById("financecost");
var deposit = document.getElementById("deposit");
var hireweekly = document.getElementById("hireweekly");
var principal = amount.value - deposit.value;
var interest = principal / 100 * 5.43;
var payments = parseFloat(period.value);
// compute the monthly payment figure
var monthly = principal / period.value;
if (isFinite(monthly)){
payment.innerHTML = monthly.toFixed(2);
total.innerHTML = (principal + interest).toFixed(2);
financecost.innerHTML = (principal + interest - principal).toFixed(2);
hireweekly.innerHTML = (monthly / 4.34524).toFixed(2);
// Save the user's input so we can restore it the next time they visit
save(amount.value, period.value);
// Advertise: find and display local lenders, but ignore network errors
try { // Catch any errors that occur within these curly braces
getLenders(amount.value, period.value);
}
catch(e) { /* And ignore those errors */ }
// Finally, chart loan balance, and interest and equity payments
chart(principal, interest, monthly, payments, hireweekly);
}
else {
// Result was Not-a-Number or infinite, which means the input was
// incomplete or invalid. Clear any previously displayed output.
hireweekly.innerHTML = "";
payment.innerHTML = ""; // Erase the content of these elements
total.innerHTML = ""
financecost.innerHTML = "";
chart(); // With no arguments, clears the chart
}
}
</script>
的HTML
<table class="finance-calculator-table" width="100%">
<tbody class="finance-calculator-table--body">
<tr class="table-row-alternate-2"><td><p>Vehcle Price (£)</p></td>
<td>
<input class="finance-calculator--input" id="amount" onchange="calculate();" placeholder="Enter the vehicle amount"></td>
</tr>
<!--<tr><td>Annual interest (%):</td> <td><input id="apr" onchange="calculate();"></td></tr>-->
<tr class="table-row-alternate-1"><td><p>Period (years)</p></td>
<td>
<select class="finance-calculator--select" id="period" onchange="calculate();">
<option value="12">12 Months</option>
<option value="24">24 Months</option>
<option value="36">36 Months</option>
<option value="48">48 Months</option>
<option value="60">60 Months</option>
<option value="72">72 Months</option>
<option value="84">84 Months</option>
</select>
</td>
<tr class="table-row-alternate-2" width="100%">
<td><p>Deposit (£)</p></td>
<td>
<input class="finance-calculator--input" id="deposit" onchange="calculate();" placeholder="Enter an amount">
</td>
</tr>
<tr class="table-row-alternate-1">
<td><p>Weekly Payments</p></td>
<td>£<span class="output" id="hireweekly"></span></td>
</tr>
<tr class="table-row-alternate-2">
<td><p>Monthly Payments</p></td>
<td>£<span class="output" id="payment"></span></td>
</tr>
<tr class="table-row-alternate-1">
<td><p>Total Payment</p></td>
<td>£<span class="output" id="total"></span></td>
</tr>
<tr class="table-row-alternate-2">
<td><p>Finance Cost</p></td>
<td>£<span class="output" id="financecost"></span></td>
</tr>
<tbody>
</table>
与错误盯着,因为与ID没有元素这种说法并不返回任何值量。
var amount = document.getElementById("amount");
在MDN文档中了解有关ID的更多信息,例如如何在HTML中定义ID,然后如何通过其ID在JavaScript中获取Elements。
话虽如此,对于您的问题有2种可能的解决方案。
要通过Liquid在液体文件中的某个位置直接输出到JS变量,您可以执行以下操作,然后在JavaScript中将productPrice包含在productPrice变量中。
<script>
var productPrice = {{ product.price}}
</script>
您还可以将ID添加到您的标记中,例如,
<p id="amount">{{ price}}</p>
然后在JavaScript中
// to get amount
var amount = document.getElementById("amount").innerText;
现在,您应该了解使其与Shopify一起使用的一些知识是..
{{amount}}变量并非在所有模板上都可用。因此请确保您在相应地使用它。
{{amount}}将以美分返回价格,因此请在计算时注意这一点。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句