Django Javascript Ajax-提交表单不起任何作用
我有一个表单,我想用它将数据发送到我的后端视图Django Javascript Ajax-提交表单不起任何作用,javascript,python,django,ajax,forms,Javascript,Python,Django,Ajax,Forms,我有一个表单,我想用它将数据发送到我的后端视图 <form class="modal-content animate" method="POST" id="make_admin"> {% csrf_token %} <!-- List users to make admin --> <div class="container"> <l
<form class="modal-content animate" method="POST" id="make_admin">
{% csrf_token %}
<!-- List users to make admin -->
<div class="container">
<label for="uname"><b>User: </b></label>
<select id="select_user">
{% for employee in users %}
{% if not employee.is_admin %}
<option value="{{ employee.id }}">{{ employee.first_name }} {{ employee.last_name }}</option>
{% endif %}
{% endfor %}
</select>
<button type="submit">Make Admin</button>
</div>
</form>
我的问题是,当点击submit按钮时,什么都没有发生,页面似乎只是在刷新。看起来javascript代码似乎永远无法访问。你知道为什么吗?提前谢谢
编辑:如果我添加
window.alert(“hello”)代码>到javascript代码无法访问它。认为错误在于html或$(文档)。在('submit','make#u admin',函数(e){
行。您正在选择没有提交操作的单击文档
选择表单元素而不是文档元素
$(文档).ready(函数(){
$('form')。关于('submit','make#u admin',函数(e){
e、 预防默认值();
$.ajax({
方法:'POST',
url:“{%url”cerberus_mvp:make_admin“%}”,
数据:{
id:$('#选择用户').val(),
csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]')。val(),
行动:"邮政"
},
成功:功能(响应){
$(“.update”).html(响应)
},
错误:函数(xhr、errmsg、err){
log(xhr.status+“:“+xhr.responseText);//向控制台提供有关错误的更多信息
}
});
})
{%csrf_令牌%}
用户:
{用户%%中的员工为%}
{%如果不是employee.is_admin%}
{{employee.first_name}{{employee.last_name}}
{%endif%}
{%endfor%}
做管理员
当您试图附加事件时,表单是否存在?换句话说,document.getElementById('make_admin'))您正在测试哪个浏览器?在提交处理程序帮助的末尾添加是否返回false;
?是否有类为update
的元素?控制台中没有收到错误消息?回答以下问题:浏览器是chrome,有一个元素“update”,它是一个空div,显示类似“Success”的内容,没有错误消息。您的浏览器控制台是否显示任何错误?
$(document).on('submit', '#make_admin' ,function(e){
e.preventDefault();
$.ajax({
type:'POST',
url:'{% url 'cerberus_mvp:make_admin' %}',
data:{
id: $('#select_user').val(),
csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val(),
action: 'post'
},
success:function(response){
$(".update").html(response)
},
error : function(xhr,errmsg,err) {
console.log(xhr.status + ": " + xhr.responseText); // provide a bit more info about the error to the console
}
});
});