我有一個問題,我的代碼,因爲我通常使用(對於按鈕,例如)的ID來指代元件,如:如何引用幾個HTML元素,用於JavaScript/jQuery函數?
$(document).ready(function() {
$('.delete_imgs').click(function() {
id = $(this).attr('id');
$('.confirm_delete').css({
visibility: "visible"
});
});
});
所以上面的存在,如果任何.delete_imgs
按鈕點擊,然後執行該功能。並且還使用this
函數將指向HTML文檔中被點擊的元素。
上面id
「s是一個PHP函數這裏:
while($stmt -> fetch()){
echo "<tr><td>
<img class='delete_imgs' id='$matchid' src='Images/delete_icon.png'>
<img class='edit_imgs' id='$matchid'src='Images/edit_icon.png'></td>
<td>$hero</td>
<td>$result</td>
<td>$gamemode</td>
<td>$mmr</td>
</tr>";
}
這是精中加入edit_imgs
圖像之前,由於所有的delete_imgs
將具有不同的ID(匹配ID是唯一的) 。但是現在,我不應該同時編輯和刪除具有相同ID的圖像,所以我不確定如何將它們引用到JavaScript/jQuery函數。
我道歉,如果這是一個相當寬泛的問題,我可以根據需要添加更多的信息:
較短版本的問題:你如何參考幾個HTML元素,而無需使用ID或類?
編輯:這是我最終將要使用的ID爲:
function deletematch(){
$.ajax({
type:"POST",
url:"php/RemoveMatch.php", //This code sends the variable with the match
data: "match_id=" + id, //id in it to the php file and executes the deletion.
success: function(){
alert("The match has been deleted");
$('.confirm_delete').css({visibility:"hidden"});
}
});
}
的editmatch尚未編碼。
可以指定在jQuery選擇多個匹配:$( 'delete_imgs,.edit_img')' – hindmost
@Hindmost但問題',是我需要做2個獨立功能,根據您是否點擊「刪除」圖像或「編輯」圖像執行。這兩個函數都需要有一個重要的變量,即「id」變量,它指的是隨後刪除/編輯的數據。我給他們這個變量的方式是用ID/Classes,但是如果沒有重複的ID,我就不能這麼做。 – k4kuz0
您可以使用重複的ID,這不會被棄用。 – hindmost