对字段值创建无限循环
所以我在 bigcartel 上创建了一家商店,我想添加免费送货选项。但他们目前不允许您将其添加到特定国家/地区。我目前正在添加编码以显示 div 或在用户的总额超过 85 英镑并且选择英国作为目的地时提醒用户。
ATM 我认为我的代码不会循环.. 好吧,它似乎不会:/ 我需要它循环,以便在客户添加或减去商品时检查总价的值..这将+或-总金额。
我已经
var amount = {{ cart.total }} ;
var country = {{ store.country | country_select }};
t=setTimeout("checkprice()",10);
function checkprice()
{
if(amount >= 85 || country = 45 )
{
alert('OVER 85!') ;
}
else
{
alert('monkeys')
}
}
编辑了! 2012 年 2 月 8 日,
按照 @kolink 的建议:我想出了这个,,,
//----get variables--
var amount = {{ cart.total }};
var moose = document.getElementById("country");
function freedel()
{
if (moose = 42 || amount >= 85 )
{
document.getElementById("moose").style.visibility='visible';
}
else
{
alert('WRONG!')
}
}
HTML 是:
<div id="moose" style="visibility:hidden;"> dfgsdgfdsg</div>
和
<h3 id="cart_price" onChange="freedel()">{{ cart.total | money_with_sign }}</h3>
So im creating a shop on bigcartel and i want to add a free delivery option. but they currently dont allow you to add it to spercific countries. I am currently adding coding to show a div or alert the user when their total is over £85 and they select UK as their desitination.
ATM i think my code wont loop.. well it dont seem to :/
I need it to loop so that it checks the value of the total price when customers add or subtract items .. whicvh would + or - the total amount.
i HAVE
var amount = {{ cart.total }} ;
var country = {{ store.country | country_select }};
t=setTimeout("checkprice()",10);
function checkprice()
{
if(amount >= 85 || country = 45 )
{
alert('OVER 85!') ;
}
else
{
alert('monkeys')
}
}
EDIT! 08/Feb/12
as suggested by @kolink: i have come up with this,,,
//----get variables--
var amount = {{ cart.total }};
var moose = document.getElementById("country");
function freedel()
{
if (moose = 42 || amount >= 85 )
{
document.getElementById("moose").style.visibility='visible';
}
else
{
alert('WRONG!')
}
}
the HTML being:
<div id="moose" style="visibility:hidden;"> dfgsdgfdsg</div>
and
<h3 id="cart_price" onChange="freedel()">{{ cart.total | money_with_sign }}</h3>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你告诉它在页面加载后 10 毫秒检查价格,然后你就永远不会告诉它再次运行。
如果您的代码有效,它会显示警报,然后再过 10 毫秒(这远远不够解决问题的时间)。
您应该做的是将
checkprice();
添加到您的代码中,无论您在哪里更新cart.total
或store.country
。也许onChange
事件可以帮助您。附带说明一下,永远不要在
setTimeout
中使用字符串。传递函数本身(即setTimeout(checkprice,10);
)或匿名函数(即setTimeout(function() {checkprice();},10);
相反。You tell it to check the price 10 milliseconds after the page loads, then you never tell it to run again.
And if your code worked, it would show the alert, then another 10 milliseconds later (which is nowhere near enough time to fix the issue).
What you should do is add
checkprice();
to your code wherever you updatecart.total
orstore.country
. Maybe anonChange
event can help you there.As a side note, never ever use a string in
setTimeout
. Pass the function itself (ie.setTimeout(checkprice,10);
) or an anonymous function (is.setTimeout(function() {checkprice();},10);
instead.