我試圖抓住被點擊的圖像的id,然後提交它的形式(通過設置隱藏的imagenr輸入的val)。但是我不知道如何拉ID。選擇圖像ID
$(document).ready(function() {
$(".test-img").click(function() {
var imagevar = // How to grab the clicked img id?
$("#imagenr").val(imagevar); // set the img id to the hidden input
$("form#test-form").submit(); // submit the form
})
})
<form action="#" method="post" id="test-form">
<input type="hidden" name="imagenr" id="imagenr" value="">
<img class="test-img" id="1" src="images/image1.jpg" alt=""/>
<img class="test-img" id="2" src="images/image2.jpg" alt=""/>
</form>