我有以下HTML:
<div id="unit0">
<input onclick="getFeedback()" id="46003" class="btn" type="button" value="Upload assignment 1 here">
<input onclick="getFeedback()" id="46001" class="btn" type="button" value="Upload assignment 2 here">
</div>
和我的getFeedback函数
function getFeedback(){
console.log("This function's caller was " + event.target.id);
$.get('moodle/api.php',{action: "getfeedback", id: event.target.id}, function(data) {
});
}
如何从该功能中选择div?我想将$ .get的输出附加到包含event.target.id
的div答案 0 :(得分:2)
您可以先将this
传递给该函数,例如:
<input onclick="getFeedback(this)" id="46003" class="btn" type="button" value="Upload assignment 1 here">
<input onclick="getFeedback(this)" id="46001" class="btn" type="button" value="Upload assignment 2 here">
接下来修改js,如:
function getFeedback(obj) {
console.log("This function's caller was " + obj.id);
$.get('moodle/api.php', {
action: "getfeedback",
id: obj.id
}, function (data) {
// Get the parent div like
var $div = $(obj).closest('div');
});
}
答案 1 :(得分:0)
您可以使用$.parent
,如下所示:
var parent_div = $(event.target).parent();
答案 2 :(得分:0)
尝试不使用内联onClick
<div id="unit0">
<input id="46003" class="btn" type="button" value="Upload assignment 1 here">
<input id="46001" class="btn" type="button" value="Upload assignment 2 here">
</div>
JS:
$('input.btn').on('click',function(e){
$.get('moodle/api.php',{action: "getfeedback", id: this.id}, function(data) {
});
});