2017-04-02 54 views
0

我在頁面上有一系列下拉按鈕,並試圖用按鈕的ID來觸發單個函數。爲什麼我的函數沒有被調用?在下拉菜單中使用按鈕觸發jquery

<div class="dropdown"> 
    <button class="btn btn-secondary dropdown-toggle btn-sm btn-block" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> 
    Options 
    </button> 
    <div class="dropdown-menu" aria-labelledby="dropdownMenuButton"> 
    <a class="dropdown-item" href="lists/{{ $list['id'] }}/details">Edit Details</a> 
    <a class="dropdown-item" href="#">Convert to...</a> 
    <a class="dropdown-item" href="#" id= "{{ $list['id'] }}" class="copy-row">Copy</a> 
    </div> 
</div> 

和JS:

$('.copy-row').click(function(e) { 

    e.preventDefault(); 
    var token = $('meta[name="csrf-token"]').attr('content'); 

    var id = $(this).attr('id'); 
    $.ajax({url: '/lists/' + id, data: {_token:token}, type: 'PUT'}) 
    window.location.reload(); 

}); 

回答

0

我有兩個單獨的類聲明:

<a class="dropdown-item" href="#" id= "{{ $list['id'] }}" class="copy-row">Copy</a> 

改爲

<a class="dropdown-item copy-row" href="#" id= "{{ $list['id'] }}">Copy</a>