2017-07-08 121 views
0

您好我需要id變量從以下超鏈接傳遞給jQuery的點擊功能傳遞變量jQuery的點擊功能

這裏是鏈接

<a id='howdy' href='#' course_id = 5>Howdy</a> 

這裏是我嘗試點擊功能利用:

$('a#howdy').click(function(e){ 
    $.get('insert_courses.php' + data.course_id, function(data){ 
    modal.open({content: data}); 
    }); 
}); 

我需要正確的語法到位的

+ data.course_id 

,因爲這似乎並沒有工作,感謝

+0

兩個時間'id'是invalid.Use'數據id'爲第二-一 –

回答

0

改變當前代碼

$('a#howdy').click(function(e){ 
    $.get('insert_courses.php?id=' + $(this).attr("course_id"), function(data){ 
    modal.open({content: data}); 
    }); 
}); 

在這裏,你去的jsfiddle https://jsfiddle.net/4asa95cw/1/

0

您只需如果元素訪問course-id屬性。

<a id='howdy' href='#' course_id = 5>Howdy</a> 

    $('a#howdy').click(function(e){ 
      $.get('insert_courses.php' + $('#howdy).attr('course-id'), function(data){ 
      modal.open({content: data}); 
      }); 
    }); 
0

那麼,你最好應使用data(),來獲得相關的屬性。這是使用JQuery的正確方式,也是您可以在DOM中設置/獲取其他屬性的方式。檢查API的更多詳細信息:https://api.jquery.com/data/

及以下更新的代碼,用data()

$('a#howdy').click(function(e) { 
 
    console.log($(this).data("course-id")); 
 
    $.get('insert_courses.php?id=' + $(this).data("course-id"), function(data) { 
 
    modal.open({ 
 
     content: data 
 
    }); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<a id='howdy' href='#' data-course-id=5>Howdy</a>