Javascript 未捕获的语法错误:
我试图在输入[type='checkbox']时提交单击表单函数,而不使用ajax jquery刷新页面。顺便说一句,我正在开发一个Django web应用程序 这是我的表单元素代码-Javascript 未捕获的语法错误:,javascript,python,django,ajax,firebase,Javascript,Python,Django,Ajax,Firebase,我试图在输入[type='checkbox']时提交单击表单函数,而不使用ajax jquery刷新页面。顺便说一句,我正在开发一个Django web应用程序 这是我的表单元素代码- {% if vendor.accountEnabled %} <form id="form" action="{% url 'change_account_switch' vendor.id %}" meth
{% if vendor.accountEnabled %}
<form id="form" action="{% url 'change_account_switch' vendor.id %}"
method="post">
{% csrf_token %}
<div class="custom-control custom-switch">
<input onclick="switchAcc('{{ vendor.id }}customSwitch1');"
type="checkbox"
name="switch"
class="custom-control-input" id="{{ vendor.id }}customSwitch1"
checked>
<label class="custom-control-label" for="{{ vendor.id }}customSwitch1">Enable</label>
</div>
</form>
{% else %}
<form id="form" action="{% url 'change_account_switch' vendor.id %}"
method="post">
{% csrf_token %}
<div class="custom-control custom-switch">
<input onclick="switchAcc('{{ vendor.id }}customSwitch1');"
type="checkbox"
name="switch"
class="custom-control-input" id="{{ vendor.id }}customSwitch1">
<label class="custom-control-label" for="{{ vendor.id }}customSwitch1">Enable</label>
</div>
</form>
{% endif %}
Django views.py--
@login_required(login_url='/login/')
def change_account_switch(request, doc_id):
ref = db.collection('vendorUsers').document(doc_id)
switch = request.GET.get('status', None)
if switch == 'true':
switch = True
elif switch == 'false':
switch = False
ref.update({
'accountEnabled': switch,
})
data = {
'is_done': ref.get().to_dict()['accountEnabled'],
}
return JsonResponse(data)
我还有一个问题。看看我的表单元素代码,我显示了多个用户,每个用户的配置文件数据库中都包含accountEnabled字段 现在,看看myviews.py代码。在该文件中,我正在根据切换按钮复选框更新accountEnabled字段
但问题是,列表中只有第一个用户的accountEnabled字段正在更新 您需要使用在函数中传递参数,这是当前出现错误的原因。您错过的其他错误,在}附近。错误,然后您以错误的方式调用了表单的操作,它应该是$'form'。attr'action'和这个{url:'/ajax/change\u account\u switch/{{doc\u id}',}也给出了错误 工作jquery代码: 函数开关accdoc\u id{ console.logvendor\u id=+doc\u id;//您的id 让switchBtn=$'input[name=switch]'。选中; console.logform_action=+$'form'.attr'action'; $.ajax{ //{url:'/ajax/change_account_switch/{{doc_id}}',} url:$'form'.attr'action',//表单操作 数据:{ “状态”:switchBtn }, 数据类型:“json”, 成功:functiondata{ console.logdata 如果data.u完成了{ 警报具有此用户名的用户已存在。; } },/,已丢失 错误:functione{ //console.loge.message; } }; } {%csrf_令牌%} 使可能
错误消息的输入标记在哪里?我还有一个问题。看看我的表单元素代码,我显示了多个用户,每个用户的配置文件数据库中都包含accountEnabled字段。现在,看看myviews.py代码。在该文件中,我正在根据切换按钮复选框更新accountEnabled字段。但问题是,列表中只有第一个用户的accountEnabled字段正在更新。我已经根据您的答案@Swati更新了上述问题。请看问题的底部并尝试回答。我对django不太了解,但要更新特定的复选框,您还需要将cb_id传递到后端页面,以仅更新其cb_id被选中的复选框。好的,我将尝试@Swati。谢谢
@login_required(login_url='/login/')
def change_account_switch(request, doc_id):
ref = db.collection('vendorUsers').document(doc_id)
switch = request.GET.get('status', None)
if switch == 'true':
switch = True
elif switch == 'false':
switch = False
ref.update({
'accountEnabled': switch,
})
data = {
'is_done': ref.get().to_dict()['accountEnabled'],
}
return JsonResponse(data)