csdn产品小助手 2015-08-07 05:30 采纳率: 0%
浏览 26

点击发送数据

I'm using Ajax (along with Django) to perform some action on button click. Before I even get to the view, I am unable to pass data to the javascript function. What is going wrong? How can I pass the button's ID data to the function? I get the message that my request_data is Undefined.

template.html

<button class="btn-green" onclick="request_access()" id="{{ data }}">Join Group</button>

javascript.js

function request_access(){
    console.log("button clicked");
    var request_data = $(this).attr('id');
    console.log("data: " + request_data);
    $.post({
        url: "request_access/",
        data : { request_data: request_data},
        success : function(json) {
            $("#request-access").hide();
            console.log("requested access complete");
        }
    })
}
  • 写回答

4条回答 默认 最新

  • 狐狸.fox 2015-08-07 05:33
    关注

    Bind the onclick event using jquery also, like this:

    jQuery(function($){
        $('.btn-green').on('click', function(e) {
              var request_data = $(this).attr('id');
              $.post .......
              .....
        })
    })
    

    and get rid of the onclick="request_access()" in html

    ALSO: Maybe you haven't pass "data" to the django template. Can you show us the generated html AND the django view function?

    评论

报告相同问题?