【发布时间】:2012-08-08 11:30:38
【问题描述】:
如果所选产品在检查此问题的方法上返回 true,我目前正在尝试使用 javascript 显示额外的表单字段。虽然我为表单选择设置了 onchange 部分,但我正在努力实际访问产品方法。我假设我必须使用我没有经验的 json。我希望执行以下操作,即在 if 语句中使用该方法的返回值。我目前尝试使用的代码是(我意识到它只是显示一个警报 atm,但我的首要任务是让它工作):
function openingCheck(optionValue){
{% check_opening_date as json %}
var checkOpeningDateJSON = {{json|safe}};
if(checkOpeningDateJSON)
alert(optionValue);
};
用途:
@register.tag(name="check_opening_date")
def check_opening_date(parser, token):
"""return whether the product should show the opening date or not"""
product = Product.objects.get(pk=productID)
return serializers.serialize('json',product.show_opening_date())
我遇到的问题是我不确定如何将 optionValue 传递给 check_opening_date 函数以及如何返回 product.show_opening_date() 的值。
任何提示将不胜感激。
【问题讨论】:
-
您是否尝试在加载页面后从服务器异步抓取数据,然后根据该数据显示表单字段?
-
我想是的。我希望当用户选择 show_opening_date 返回 true 的产品时,我将显示用于输入开业日期的表单字段。因为并非所有产品都返回 true 我的猜测是必须使用json来找出产品是否返回true,然后根据需要显示表单字段。如果这是有道理的。 (我不确定这是否可能是公平的)。
-
show_opening_date是Product模型上的方法? -
确实(它检查产品上的值,只返回 True 或 False)
标签: javascript django json