Django button ajax click

后端 未结 1 1254
-上瘾入骨i
-上瘾入骨i 2021-01-21 11:15

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 butt

相关标签:
1条回答
  • 2021-01-21 11:56

    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>
    
    0 讨论(0)
提交回复
热议问题