0

js と ajax を呼び出してレコードを削除します。

     $('.delete').click(function (e) {
       if (confirm("Are you sure want to delete record?")) {
           return true;
       }
       else {

           e.preventDefault();

       }
   });


        $('.type_delete').click(function() {
        var csrf_token = $("#csrf_token").val();
        var id = $(this).attr('id');
        $.ajax({ // create an AJAX call...
            data:{
                csrfmiddlewaretoken: ('{{csrf_token}}'),
                delete:id
            },
            type:'POST',
            url: '/setting/type/', // the file to call
            cache:false,
            success: function() { // on success..
                window.location.href = window.location;
            }
        });
        return false;
    });
    });

ビュー.py

def types(request):
    if request.method == 'POST':
        if 'delete' in request.POST:
            Types.objects.filter(id=request.POST['delete']).delete()
    """""""""
    return render

html:

 <input type="button" name="delete"  value="Delete" id="{{ type.id }}" class="type_delete delete"/>

上記の ajax は、html から正しい ID を取得し、その特定のデータを削除することです。 [OK] を押した場合にのみ発生します。この問題を解決するには助けが必要です。

4

2 に答える 2

1

確認ボックスが表示されるまでに、2 番目の関数の呼び出しが既に進行中です。2 番目の関数を別の独立した関数として定義し、最初の関数内から呼び出す必要があります。

次に例を示します。

 $('.delete').click(function (e) {

   e.preventDefault();
   if (confirm("Are you sure want to delete record?")) {
       doDelete($(this).attr('id'))
   }
 });




 function doDelete(elemId) {
    var csrf_token = $("#csrf_token").val();
    var id = elemId;
    $.ajax({ // create an AJAX call...
        data:{
            csrfmiddlewaretoken: ('{{csrf_token}}'),
            delete:id
        },
        type:'POST',
        url: '/setting/type/', // the file to call
        cache:false,
        success: function() { // on success..
            window.location.href = window.location;
        });
    }
于 2013-09-02T17:56:29.703 に答える
0

1 つのイベント ハンドラーの条件が 2 番目のイベント ハンドラーで機能するとは限りません。1 つのイベント ハンドラーですべてを結合する必要があります。

$('.type_delete').on('click', function (e) {
    e.preventDefault();
    if (confirm("Are you sure want to delete record?")) {
        var csrf_token = $("#csrf_token").val(),
            id         = this.id;
        $.ajax({
            data: {
                csrfmiddlewaretoken: '{{'+csrf_token+'}}',
                delete: id
            },
            type: 'POST',
            url: '/setting/type/',
            cache: false
        });
    }
});
于 2013-09-02T17:52:32.623 に答える