Django按钮ajax点击 [英] Django button ajax click

查看:178
本文介绍了Django按钮ajax点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些HTML,Django呈现良好。我想单击HTML上的一个按钮,并导致事件在视图中触发。



看起来不像按钮导致 post 发生。我不知道我做错了什么



这是我的 views.py 代码:

  def log(request):
if not request.POST:
template = loader.get_template('log.html')
html = template。渲染(Context())
返回HttpResponse(html)
打印发布

这是我的 log.html

 <!DOCTYPE HTML> 
< html>
< head>
< title>< / title>
< script type =text / javascript>
$(#update_log_button)bind(button,function(){
$ .post(hello);
return false;
});
< / script>
< / head>
< body>
< p>
< span> SPHIINX日志< / span>
< / p>
< p>
< textarea cols =2name =logrows =25>< / textarea>
< input id =update_log_buttonname =update_logtype =buttonvalue =更新日志/>
< / p>
< / body>
< / html>


解决方案

是否有原因为什么不使用直接点击事件



尝试这样:

 < script type =text / javascript> 
$(function(){
$(#update_log_button)。click(function(){
$ .post(
url:/ hello /,
dataType:html,
success:function(data,status,xhr){
// do something with your data
}
);
return false;
});
});
< / script>

如果按钮是动态创建的,那么您可能需要使用 bind 函数附加单击事件处理程序到元素:

 < script type =text / javascript> 
$(function(){
$(#update_log_button)。bind('click',function(){
$ .post(
url:/ hello / ,
dataType:html,
success:function(data,status,xhr){
// do something with your data
}
);

return false;
});
});
< / script>

尝试此URL以包含JQuery库:

 < script src =http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js>< / script> ; 


I have some HTML that Django is rendering well. I'd like to click a button on the HTML and have that cause an event to fire in the view.

It doesn't look like the button is causing the post to fire. I'm not sure what I'm doing wrong.

This is my views.py code:

def log(request):
  if not request.POST:
    template = loader.get_template('log.html')
    html = template.render(Context())
    return HttpResponse(html)
  print "Post"

This is my log.html:

<!DOCTYPE html>
<html>
<head>
    <title></title>
<script type="text/javascript">
    $("#update_log_button").bind("button", function(){
        $.post("hello");
        return false;
    });
</script>
</head>
<body>
<p>
    <span>SPHIINX Log</span>
</p>
<p>
    <textarea cols="2" name="log" rows="25"></textarea>
     <input id="update_log_button" name="update_log" type="button" value="Update Log"/>
</p>
</body>
</html>

解决方案

Is there a reason Why you are not using the click event directly?

Try this:

<script type="text/javascript">
  $(function(){
    $("#update_log_button").click(function(){
        $.post(
         url : "/hello/",
         dataType:"html",
         success: function(data, status, xhr){
            //do something with your data
        }
        );
        return false;
    });
 });
</script>

If the button is dynamically created then you may want to use the bind function to attach click event handler to the element:

<script type="text/javascript">
   $(function(){
    $("#update_log_button").bind('click', function(){
        $.post(
         url : "/hello/",
         dataType:"html",
         success: function(data, status, xhr){
            //do something with your data
        }
        );

        return false;
    });
   });
</script>

Try this URL to include the JQuery library:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

这篇关于Django按钮ajax点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆